# Liste des packages à installer
#packages <- c("officer", "flextable", "tidyverse", "summarytools", 
              #"gtsummary", "survey", "readxl", "dplyr", "ggplot2", "scales","cardx")

# Installer les packages
#install.packages(packages)

# Vérifier si les packages sont correctement installés et les charger
#lapply(packages, require, character.only = TRUE)



library(cardx)
library(officer)
library(flextable)
library(tidyverse)
library(summarytools)
library(gtsummary)
library(survey)
library(readxl)
library(survey)
library(dplyr)
library(ggplot2)
library(scales)

#chooseCRANmirror()
#11
#options(repos = c(CRAN = "https://cran.rstudio.com/"))
#install.packages("here")
library(here)
data_P3_ = read_csv(here("data", "data_P3.csv"))
  1. Ponderation
# Créer l'objet de design en tenant compte des pondérations
data_design <- svydesign(id = ~id, weights = ~PON, data = data_P3_)

1. Tableau pour les Répondants

table_repondants <- tbl_svysummary(
  data = data_design,
  by = gpe_age, 
  include = c(age, sexe,Lieu_de_residence,lieu_de_naissance,Diplome_du_repondant, Occupation_du_repondant),
  
  statistic = list(
    all_continuous() ~ "{mean}",
    all_categorical() ~ "{p}   ({n})"
  ),
  digits=~ c(1,0),
  missing = "no"  # ne pas afficher les valeurs manquantes
) %>%
  add_overall() %>%  # ajouter la colonne « Ensemble »
  add_p() %>%        # ajouter les p-values pour tester la différence entre les groupes
  modify_header(label = "**Caractéristiques**") %>% 
  modify_caption("**Répondants**")


table_repondants
Répondants
Caractéristiques Overall
N = 721
1
18-24 ans
N = 268
1
25-34 ans
N = 453
1
p-value2
age 26.4 21.1 29.6 <0.001
sexe


0.9
    Femme 49.8 (359) 49.4 (132) 50.1 (227)
    Homme 50.2 (362) 50.6 (136) 49.9 (226)
Lieu_de_residence


>0.9
    Ailleurs au Quebec 54.8 (395) 55.0 (147) 54.6 (247)
    Grand Motreal 45.2 (326) 45.0 (121) 45.4 (205)
lieu_de_naissance


0.11
    Hors du Canada 6.7 (48) 7.2 (19) 6.3 (29)
    Hors du Quebec 1.1 (8) 2.3 (6) 0.4 (2)
    Provinde de Quebec 92.2 (662) 90.4 (241) 93.2 (421)
Diplome_du_repondant


<0.001
    CEGEP / DEP 48.2 (347) 48.3 (128) 48.2 (218)
    Primaire/Secondaire 20.6 (148) 32.8 (87) 13.4 (61)
    Universitaire 31.2 (224) 18.9 (50) 38.3 (174)
Occupation_du_repondant


<0.001
    À la maison TPL 3.7 (26) 2.5 (7) 4.4 (20)
    Étudiant PTL S 8.5 (61) 17.9 (47) 3.0 (13)
    Étudiant TPA S 0.5 (3) 0.7 (2) 0.3 (2)
    Sans travail mais recherche 3.1 (22) 1.8 (5) 3.8 (17)
    Sans travail sans recherche 1.0 (7) 0.0 (0) 1.6 (7)
    Travail et études TPA 0.9 (6) 1.4 (4) 0.6 (3)
    Travail PTL études TPA 6.3 (45) 5.7 (15) 6.7 (30)
    Travail TPA études PTL 15.1 (108) 36.5 (97) 2.6 (12)
    Travail TPA S 4.9 (35) 7.2 (19) 3.6 (16)
    Travail TPL S 56.1 (402) 26.4 (70) 73.6 (332)
1 Mean; % (n)
2 Design-based KruskalWallis test; Pearson’s X^2: Rao & Scott adjustment

1. Tableau pour l’utilisation des médias sociaux des repondants

table_utilisation_medias_sociaux <- 
  data_design %>%
  tbl_svysummary(
    by = gpe_age,
    include = c(Usage_MS,Nbr_compte_MS,Nbr_compte_MS_regulier),
    statistic = list(
    all_continuous() ~ "{mean}",
    all_categorical() ~ "{p} ({n})"
  ),
  digits=~ c(1,0),
  missing = "no"  
) %>%
  add_overall() %>%  
  add_p() %>%        
  modify_header(label = "**Caractéristiques**") %>% 
  modify_caption("**Utilisation_médias_sociaux**")

table_utilisation_medias_sociaux
Utilisation_médias_sociaux
Caractéristiques Overall
N = 721
1
18-24 ans
N = 268
1
25-34 ans
N = 453
1
p-value2
Usage_MS


<0.001
    Non 8.4 (60) 14.4 (39) 4.8 (22)
    Oui 91.6 (660) 85.6 (229) 95.2 (431)
Nbr_compte_MS


<0.001
    1 ou 2 comptes 27.3 (197) 10.1 (27) 37.5 (170)
    3 à 5 comptes 53.1 (383) 56.0 (150) 51.3 (232)
    6 comptes et plus 10.4 (75) 17.9 (48) 6.0 (27)
    Aucun 9.2 (66) 15.9 (43) 5.2 (23)
Nbr_compte_MS_regulier


<0.001
    Aucun 9.5 (68) 15.2 (41) 6.1 (28)
    Deux 30.1 (217) 20.9 (56) 35.6 (161)
    Plus de quatre 7.0 (50) 13.0 (35) 3.4 (16)
    Quatre 11.1 (80) 17.5 (47) 7.3 (33)
    Trois 23.9 (172) 25.7 (69) 22.9 (103)
    Un 18.4 (132) 7.6 (20) 24.7 (112)
1 % (n)
2 Pearson’s X^2: Rao & Scott adjustment

3 Activités faites sur les médias sociaux

Table_activités_médias_sociaux <- 
  data_design %>%
  tbl_svysummary(
    by = gpe_age,
    include = c(Suivre_activité_amis_et_famille, Communiquer_amis_et_famille, Partager_propre_matériel_avec_proches, Partager_propre_matériel_publiquement, Suivre_actualités_sur_RSM, Prendre_connaissance_programmes_gouv, Rechercher_information_generale, Rechercher_info_importante_personnelle, Aucune, Autre),
    
    statistic = list(
    all_continuous() ~ "{mean}",
    all_categorical() ~ "{p} ({n})"
  ),
  digits=~ c(1,0),
  missing = "no"  # ne pas afficher les valeurs manquantes
) %>%
  add_overall() %>%  
  add_p() %>%        
  modify_header(label = "**Caractéristiques**") %>% 
  modify_caption("**Activités médias sociaux**")

Table_activités_médias_sociaux
Activités médias sociaux
Caractéristiques Overall
N = 721
1
18-24 ans
N = 268
1
25-34 ans
N = 453
1
p-value2
Suivre_activité_amis_et_famille


0.002
    Non 25.2 (182) 32.7 (88) 20.8 (94)
    Oui 74.8 (539) 67.3 (180) 79.2 (358)
Communiquer_amis_et_famille


0.5
    Non 26.7 (193) 28.4 (76) 25.8 (117)
    Oui 73.3 (528) 71.6 (192) 74.2 (336)
Partager_propre_matériel_avec_proches


0.4
    Non 64.2 (463) 66.3 (178) 62.9 (285)
    Oui 35.8 (258) 33.7 (90) 37.1 (168)
Partager_propre_matériel_publiquement


0.010
    Non 76.2 (549) 70.4 (189) 79.6 (360)
    Oui 23.8 (172) 29.6 (79) 20.4 (92)
Suivre_actualités_sur_RSM


0.024
    Non 51.8 (373) 57.7 (155) 48.3 (219)
    Oui 48.2 (347) 42.3 (113) 51.7 (234)
Prendre_connaissance_programmes_gouv


0.7
    Non 86.0 (620) 85.4 (229) 86.4 (391)
    Oui 14.0 (101) 14.6 (39) 13.6 (61)
Rechercher_information_generale


0.009
    Non 64.9 (467) 58.3 (156) 68.8 (311)
    Oui 35.1 (253) 41.7 (112) 31.2 (141)
Rechercher_info_importante_personnelle


0.2
    Non 82.6 (596) 80.1 (215) 84.1 (381)
    Oui 17.4 (125) 19.9 (53) 15.9 (72)
Aucune


0.5
    Non 99.5 (717) 99.7 (267) 99.3 (450)
    Oui 0.5 (4) 0.3 (1) 0.7 (3)
Autre


0.2
    Non 99.8 (720) 99.5 (267) 100.0 (453)
    Oui 0.2 (1) 0.5 (1) 0.0 (0)
1 % (n)
2 Pearson’s X^2: Rao & Scott adjustment

4. Quête d’informations

Table_Quête_informatins <- 
  data_design %>%
  tbl_svysummary(
    by =gpe_age,
    include = c(Votre_mere, Votre_pere, Votre_soeur, Votre_frere, Vos_amis, A_l_ecole, Vos_colleges_de_travail, Les_medias_traditionnels, Les_medias_numeriques_Q1),
    statistic = list(
    all_continuous() ~ "{mean}",
    all_categorical() ~ "{p} ({n})"
  ),
  digits=~ c(1,0),
  missing = "no"  
) %>%
  add_overall() %>%  
  modify_header(label = "**Caractéristiques**") %>% 
  modify_caption("**Quête_informations**")

Table_Quête_informatins
Quête_informations
Caractéristiques Overall
N = 721
1
18-24 ans
N = 268
1
25-34 ans
N = 453
1
Votre_mere


    Important 35.2 (254) 38.2 (102) 33.4 (151)
    Pas important 10.7 (77) 10.5 (28) 10.8 (49)
    Pas important du tout 5.6 (40) 3.2 (9) 7.0 (32)
    Plus ou moins important 21.0 (152) 17.6 (47) 23.1 (104)
    Très important 27.5 (198) 30.5 (82) 25.8 (117)
Votre_pere


    Important 33.6 (242) 38.4 (103) 30.8 (139)
    Pas important 7.1 (51) 4.3 (12) 8.7 (39)
    Pas important du tout 9.3 (67) 8.0 (21) 10.0 (45)
    Plus ou moins important 22.6 (163) 18.1 (49) 25.2 (114)
    Très important 27.5 (198) 31.2 (84) 25.3 (115)
Votre_soeur


    Important 24.0 (173) 23.2 (62) 24.4 (111)
    Pas important 8.2 (59) 9.8 (26) 7.3 (33)
    Pas important du tout 25.3 (182) 23.2 (62) 26.5 (120)
    Plus ou moins important 25.8 (186) 22.7 (61) 27.7 (125)
    Très important 16.7 (121) 21.0 (56) 14.2 (64)
Votre_frere


    Important 23.2 (167) 21.5 (58) 24.2 (110)
    Pas important 8.2 (59) 8.8 (23) 7.9 (36)
    Pas important du tout 27.7 (200) 30.2 (81) 26.2 (119)
    Plus ou moins important 27.1 (195) 23.8 (64) 29.0 (131)
    Très important 13.8 (100) 15.8 (42) 12.7 (57)
Vos_amis


    Important 47.1 (340) 46.7 (125) 47.4 (215)
    Pas important 7.5 (54) 7.2 (19) 7.7 (35)
    Pas important du tout 1.8 (13) 1.9 (5) 1.8 (8)
    Plus ou moins important 30.9 (222) 30.6 (82) 31.0 (140)
    Très important 12.7 (91) 13.6 (37) 12.1 (55)
A_l_ecole


    Important 39.5 (284) 43.1 (115) 37.3 (169)
    Pas important 9.4 (68) 9.5 (25) 9.4 (42)
    Pas important du tout 10.3 (74) 7.1 (19) 12.2 (55)
    Plus ou moins important 28.1 (203) 24.5 (66) 30.3 (137)
    Très important 12.7 (92) 15.9 (43) 10.8 (49)
Vos_colleges_de_travail


    Important 35.0 (253) 33.7 (90) 35.8 (162)
    Pas important 15.8 (114) 16.5 (44) 15.4 (70)
    Pas important du tout 6.7 (49) 5.9 (16) 7.2 (33)
    Plus ou moins important 35.9 (259) 37.8 (101) 34.9 (158)
    Très important 6.5 (47) 6.2 (17) 6.7 (30)
Les_medias_traditionnels


    Important 29.8 (215) 27.7 (74) 31.1 (141)
    Pas important 17.0 (122) 17.1 (46) 16.9 (76)
    Pas important du tout 9.7 (70) 6.5 (18) 11.6 (52)
    Plus ou moins important 35.6 (257) 41.3 (111) 32.3 (146)
    Très important 7.8 (56) 7.4 (20) 8.1 (37)
Les_medias_numeriques_Q1


    Important 18.9 (136) 19.8 (53) 18.4 (83)
    Pas important 24.9 (180) 24.9 (67) 24.9 (113)
    Pas important du tout 12.4 (89) 10.7 (29) 13.4 (61)
    Plus ou moins important 40.4 (291) 41.9 (112) 39.5 (179)
    Très important 3.4 (24) 2.8 (7) 3.8 (17)
1 % (n)

5. Tableau pour les Parents des repondants

Table_parents <- tbl_svysummary(
  data = data_design,
  by = gpe_age,  
  include = c( Diplome_mere, Diplome_pere,Siruation_civile_parents,Occupation_mere,Occupation_pere),
  statistic = list(
    all_continuous() ~ "{mean}",
    all_categorical() ~ "{p} ({n})"
  ),
  digits=~ c(1,0),
  missing = "no"
) %>%
  add_overall() %>%
  add_p() %>%
  modify_header(label = "**Caractéristiques**") %>%
  modify_caption("**Parents**")

Table_parents
Parents
Caractéristiques Overall
N = 721
1
18-24 ans
N = 268
1
25-34 ans
N = 453
1
p-value2
Diplome_mere


<0.001
    CEGEP / DEP 40.7 (281) 37.0 (91) 42.8 (190)
    Primaire/Secondaire 24.9 (172) 17.2 (42) 29.2 (130)
    Universitaire 34.3 (237) 45.8 (112) 28.1 (125)
Diplome_pere


0.10
    CEGEP / DEP 37.8 (258) 38.9 (95) 37.3 (163)
    Primaire/Secondaire 31.1 (212) 26.1 (64) 33.9 (148)
    Universitaire 31.1 (211) 35.0 (85) 28.8 (126)
Siruation_civile_parents


0.088
    Divorce 17.7 (126) 14.0 (37) 19.8 (89)
    Les deux sont defunts 0.7 (5) 0.5 (1) 0.8 (4)
    Separe 20.2 (145) 22.5 (59) 18.9 (85)
    Toujours ensemble 52.8 (378) 56.8 (150) 50.4 (228)
    Un des deux est defunt 8.6 (62) 6.1 (16) 10.1 (45)
Occupation_mere


<0.001
    Agriculeurs exploitants 0.4 (3) 0.4 (1) 0.4 (2)
    Artisants, commerçants, chef 5.0 (34) 6.2 (15) 4.3 (19)
    Autre sans activité professionnelle 5.6 (39) 6.4 (16) 5.1 (23)
    Cadres, intélets supérieurs 10.0 (69) 12.0 (29) 8.9 (40)
    Employés 45.1 (311) 49.1 (120) 42.9 (190)
    Ouvriers 2.4 (17) 1.1 (3) 3.2 (14)
    Préfère ne pas répondre 2.7 (18) 4.8 (12) 1.5 (7)
    Professions intermédiaires 11.6 (80) 12.4 (30) 11.2 (50)
    Retraités 17.2 (118) 7.5 (18) 22.5 (100)
Occupation_pere


<0.001
    Agriculeurs exploitants 1.9 (13) 1.4 (3) 2.1 (9)
    Artisants, commerçants, chef 7.6 (51) 9.5 (23) 6.5 (29)
    Cadres, intélets supérieurs 12.3 (83) 11.9 (29) 12.5 (55)
    Employés 36.1 (245) 42.0 (101) 32.9 (144)
    Ouvriers 7.9 (53) 8.2 (20) 7.7 (34)
    Préfère ne pas répondre 2.6 (18) 4.1 (10) 1.7 (8)
    Professions intermédiaires 9.8 (66) 11.3 (27) 8.9 (39)
    Retraités 19.7 (134) 9.1 (22) 25.6 (112)
    Sans activité profes 2.2 (15) 2.5 (6) 2.0 (9)
1 % (n)
2 Pearson’s X^2: Rao & Scott adjustment

7. Combinaison des tableaux dans un seul tableau

final_table <- tbl_stack(
  tbls = list(table_repondants,table_utilisation_medias_sociaux,Table_activités_médias_sociaux,Table_Quête_informatins,Table_parents),
  group_header = c("Répondants", "utilisation_medias_sociaux","activités_médias_sociaux","Quête_informatins","Parents")
)

# Affichage du tableau final
final_table
Répondants
Caractéristiques Overall
N = 721
1
18-24 ans
N = 268
1
25-34 ans
N = 453
1
p-value2
Répondants
age 26.4 21.1 29.6 <0.001
sexe


0.9
    Femme 49.8 (359) 49.4 (132) 50.1 (227)
    Homme 50.2 (362) 50.6 (136) 49.9 (226)
Lieu_de_residence


>0.9
    Ailleurs au Quebec 54.8 (395) 55.0 (147) 54.6 (247)
    Grand Motreal 45.2 (326) 45.0 (121) 45.4 (205)
lieu_de_naissance


0.11
    Hors du Canada 6.7 (48) 7.2 (19) 6.3 (29)
    Hors du Quebec 1.1 (8) 2.3 (6) 0.4 (2)
    Provinde de Quebec 92.2 (662) 90.4 (241) 93.2 (421)
Diplome_du_repondant


<0.001
    CEGEP / DEP 48.2 (347) 48.3 (128) 48.2 (218)
    Primaire/Secondaire 20.6 (148) 32.8 (87) 13.4 (61)
    Universitaire 31.2 (224) 18.9 (50) 38.3 (174)
Occupation_du_repondant


<0.001
    À la maison TPL 3.7 (26) 2.5 (7) 4.4 (20)
    Étudiant PTL S 8.5 (61) 17.9 (47) 3.0 (13)
    Étudiant TPA S 0.5 (3) 0.7 (2) 0.3 (2)
    Sans travail mais recherche 3.1 (22) 1.8 (5) 3.8 (17)
    Sans travail sans recherche 1.0 (7) 0.0 (0) 1.6 (7)
    Travail et études TPA 0.9 (6) 1.4 (4) 0.6 (3)
    Travail PTL études TPA 6.3 (45) 5.7 (15) 6.7 (30)
    Travail TPA études PTL 15.1 (108) 36.5 (97) 2.6 (12)
    Travail TPA S 4.9 (35) 7.2 (19) 3.6 (16)
    Travail TPL S 56.1 (402) 26.4 (70) 73.6 (332)
utilisation_medias_sociaux
Usage_MS


<0.001
    Non 8.4 (60) 14.4 (39) 4.8 (22)
    Oui 91.6 (660) 85.6 (229) 95.2 (431)
Nbr_compte_MS


<0.001
    1 ou 2 comptes 27.3 (197) 10.1 (27) 37.5 (170)
    3 à 5 comptes 53.1 (383) 56.0 (150) 51.3 (232)
    6 comptes et plus 10.4 (75) 17.9 (48) 6.0 (27)
    Aucun 9.2 (66) 15.9 (43) 5.2 (23)
Nbr_compte_MS_regulier


<0.001
    Aucun 9.5 (68) 15.2 (41) 6.1 (28)
    Deux 30.1 (217) 20.9 (56) 35.6 (161)
    Plus de quatre 7.0 (50) 13.0 (35) 3.4 (16)
    Quatre 11.1 (80) 17.5 (47) 7.3 (33)
    Trois 23.9 (172) 25.7 (69) 22.9 (103)
    Un 18.4 (132) 7.6 (20) 24.7 (112)
activités_médias_sociaux
Suivre_activité_amis_et_famille


0.002
    Non 25.2 (182) 32.7 (88) 20.8 (94)
    Oui 74.8 (539) 67.3 (180) 79.2 (358)
Communiquer_amis_et_famille


0.5
    Non 26.7 (193) 28.4 (76) 25.8 (117)
    Oui 73.3 (528) 71.6 (192) 74.2 (336)
Partager_propre_matériel_avec_proches


0.4
    Non 64.2 (463) 66.3 (178) 62.9 (285)
    Oui 35.8 (258) 33.7 (90) 37.1 (168)
Partager_propre_matériel_publiquement


0.010
    Non 76.2 (549) 70.4 (189) 79.6 (360)
    Oui 23.8 (172) 29.6 (79) 20.4 (92)
Suivre_actualités_sur_RSM


0.024
    Non 51.8 (373) 57.7 (155) 48.3 (219)
    Oui 48.2 (347) 42.3 (113) 51.7 (234)
Prendre_connaissance_programmes_gouv


0.7
    Non 86.0 (620) 85.4 (229) 86.4 (391)
    Oui 14.0 (101) 14.6 (39) 13.6 (61)
Rechercher_information_generale


0.009
    Non 64.9 (467) 58.3 (156) 68.8 (311)
    Oui 35.1 (253) 41.7 (112) 31.2 (141)
Rechercher_info_importante_personnelle


0.2
    Non 82.6 (596) 80.1 (215) 84.1 (381)
    Oui 17.4 (125) 19.9 (53) 15.9 (72)
Aucune


0.5
    Non 99.5 (717) 99.7 (267) 99.3 (450)
    Oui 0.5 (4) 0.3 (1) 0.7 (3)
Autre


0.2
    Non 99.8 (720) 99.5 (267) 100.0 (453)
    Oui 0.2 (1) 0.5 (1) 0.0 (0)
Quête_informatins
Votre_mere



    Important 35.2 (254) 38.2 (102) 33.4 (151)
    Pas important 10.7 (77) 10.5 (28) 10.8 (49)
    Pas important du tout 5.6 (40) 3.2 (9) 7.0 (32)
    Plus ou moins important 21.0 (152) 17.6 (47) 23.1 (104)
    Très important 27.5 (198) 30.5 (82) 25.8 (117)
Votre_pere



    Important 33.6 (242) 38.4 (103) 30.8 (139)
    Pas important 7.1 (51) 4.3 (12) 8.7 (39)
    Pas important du tout 9.3 (67) 8.0 (21) 10.0 (45)
    Plus ou moins important 22.6 (163) 18.1 (49) 25.2 (114)
    Très important 27.5 (198) 31.2 (84) 25.3 (115)
Votre_soeur



    Important 24.0 (173) 23.2 (62) 24.4 (111)
    Pas important 8.2 (59) 9.8 (26) 7.3 (33)
    Pas important du tout 25.3 (182) 23.2 (62) 26.5 (120)
    Plus ou moins important 25.8 (186) 22.7 (61) 27.7 (125)
    Très important 16.7 (121) 21.0 (56) 14.2 (64)
Votre_frere



    Important 23.2 (167) 21.5 (58) 24.2 (110)
    Pas important 8.2 (59) 8.8 (23) 7.9 (36)
    Pas important du tout 27.7 (200) 30.2 (81) 26.2 (119)
    Plus ou moins important 27.1 (195) 23.8 (64) 29.0 (131)
    Très important 13.8 (100) 15.8 (42) 12.7 (57)
Vos_amis



    Important 47.1 (340) 46.7 (125) 47.4 (215)
    Pas important 7.5 (54) 7.2 (19) 7.7 (35)
    Pas important du tout 1.8 (13) 1.9 (5) 1.8 (8)
    Plus ou moins important 30.9 (222) 30.6 (82) 31.0 (140)
    Très important 12.7 (91) 13.6 (37) 12.1 (55)
A_l_ecole



    Important 39.5 (284) 43.1 (115) 37.3 (169)
    Pas important 9.4 (68) 9.5 (25) 9.4 (42)
    Pas important du tout 10.3 (74) 7.1 (19) 12.2 (55)
    Plus ou moins important 28.1 (203) 24.5 (66) 30.3 (137)
    Très important 12.7 (92) 15.9 (43) 10.8 (49)
Vos_colleges_de_travail



    Important 35.0 (253) 33.7 (90) 35.8 (162)
    Pas important 15.8 (114) 16.5 (44) 15.4 (70)
    Pas important du tout 6.7 (49) 5.9 (16) 7.2 (33)
    Plus ou moins important 35.9 (259) 37.8 (101) 34.9 (158)
    Très important 6.5 (47) 6.2 (17) 6.7 (30)
Les_medias_traditionnels



    Important 29.8 (215) 27.7 (74) 31.1 (141)
    Pas important 17.0 (122) 17.1 (46) 16.9 (76)
    Pas important du tout 9.7 (70) 6.5 (18) 11.6 (52)
    Plus ou moins important 35.6 (257) 41.3 (111) 32.3 (146)
    Très important 7.8 (56) 7.4 (20) 8.1 (37)
Les_medias_numeriques_Q1



    Important 18.9 (136) 19.8 (53) 18.4 (83)
    Pas important 24.9 (180) 24.9 (67) 24.9 (113)
    Pas important du tout 12.4 (89) 10.7 (29) 13.4 (61)
    Plus ou moins important 40.4 (291) 41.9 (112) 39.5 (179)
    Très important 3.4 (24) 2.8 (7) 3.8 (17)
Parents
Diplome_mere


<0.001
    CEGEP / DEP 40.7 (281) 37.0 (91) 42.8 (190)
    Primaire/Secondaire 24.9 (172) 17.2 (42) 29.2 (130)
    Universitaire 34.3 (237) 45.8 (112) 28.1 (125)
Diplome_pere


0.10
    CEGEP / DEP 37.8 (258) 38.9 (95) 37.3 (163)
    Primaire/Secondaire 31.1 (212) 26.1 (64) 33.9 (148)
    Universitaire 31.1 (211) 35.0 (85) 28.8 (126)
Siruation_civile_parents


0.088
    Divorce 17.7 (126) 14.0 (37) 19.8 (89)
    Les deux sont defunts 0.7 (5) 0.5 (1) 0.8 (4)
    Separe 20.2 (145) 22.5 (59) 18.9 (85)
    Toujours ensemble 52.8 (378) 56.8 (150) 50.4 (228)
    Un des deux est defunt 8.6 (62) 6.1 (16) 10.1 (45)
Occupation_mere


<0.001
    Agriculeurs exploitants 0.4 (3) 0.4 (1) 0.4 (2)
    Artisants, commerçants, chef 5.0 (34) 6.2 (15) 4.3 (19)
    Autre sans activité professionnelle 5.6 (39) 6.4 (16) 5.1 (23)
    Cadres, intélets supérieurs 10.0 (69) 12.0 (29) 8.9 (40)
    Employés 45.1 (311) 49.1 (120) 42.9 (190)
    Ouvriers 2.4 (17) 1.1 (3) 3.2 (14)
    Préfère ne pas répondre 2.7 (18) 4.8 (12) 1.5 (7)
    Professions intermédiaires 11.6 (80) 12.4 (30) 11.2 (50)
    Retraités 17.2 (118) 7.5 (18) 22.5 (100)
Occupation_pere


<0.001
    Agriculeurs exploitants 1.9 (13) 1.4 (3) 2.1 (9)
    Artisants, commerçants, chef 7.6 (51) 9.5 (23) 6.5 (29)
    Cadres, intélets supérieurs 12.3 (83) 11.9 (29) 12.5 (55)
    Employés 36.1 (245) 42.0 (101) 32.9 (144)
    Ouvriers 7.9 (53) 8.2 (20) 7.7 (34)
    Préfère ne pas répondre 2.6 (18) 4.1 (10) 1.7 (8)
    Professions intermédiaires 9.8 (66) 11.3 (27) 8.9 (39)
    Retraités 19.7 (134) 9.1 (22) 25.6 (112)
    Sans activité profes 2.2 (15) 2.5 (6) 2.0 (9)
1 Mean; % (n)
2 Design-based KruskalWallis test; Pearson’s X^2: Rao & Scott adjustment

8. Créer un document Word, ajouter un titre et le tableau, puis sauvegarder le fichier

# Convertir le tableau en flextable
final_table_flex <- as_flex_table(final_table) %>%
  theme_box() %>%
  border_inner(border = fp_border(color="black", width = 1)) %>%
  border_outer(border = fp_border(color="black", width = 2))


# Créer un document Word, ajouter un titre et le tableau, puis sauvegarder le fichier
doc <- read_docx() %>%
  body_add_par("Portrait sociodémographique des répondants et de leurs parents", style = "heading 1") %>%
  body_add_flextable(final_table_flex)

# Enregistrer le document Word dans le répertoire courant
print(doc, target = here("rapport", "Table_final_caracteristiques.docx"))

9. Marqueurs objectifs selon le sexe

1: Pas important du tout 2: Pas important 3: plus ou moins important 4: important 5: Très important

table_Marqueurs_objectifs_sexe <- 
  data_design %>%
  tbl_svysummary(
    by = sexe,
    include = c(Quiter_nid, Emploi_TPL, Vivre_en_couple, Marier_1er_fois, Parent_1er_fois, sexe),
  statistic = list(
    all_continuous() ~ "{mean}",
    all_categorical() ~ "{p} ({n})"
  ),
  digits=~ c(1,0),
  missing = "no"  
) %>%
  add_overall() %>%  
  add_p() %>%        
  modify_header(label = "**Caractéristiques**") %>% 
  modify_caption("**table_Marqueurs_objectifs_sexe**")

table_Marqueurs_objectifs_sexe
table_Marqueurs_objectifs_sexe
Caractéristiques Overall
N = 721
1
Femme
N = 359
1
Homme
N = 362
1
p-value2
Quiter_nid


<0.001
    Important 44.0 (317) 45.7 (164) 42.3 (153)
    Pas du tout important 7.4 (53) 3.7 (13) 11.0 (40)
    Peu important 10.8 (78) 7.8 (28) 13.8 (50)
    Plus ou moins important 21.5 (155) 21.2 (76) 21.8 (79)
    Très important 16.3 (117) 21.6 (77) 11.1 (40)
Emploi_TPL


0.064
    Important 44.6 (321) 48.8 (175) 40.4 (146)
    Pas du tout important 2.8 (20) 1.7 (6) 4.0 (15)
    Peu important 7.8 (56) 6.2 (22) 9.4 (34)
    Plus ou moins important 22.1 (159) 19.7 (71) 24.5 (89)
    Très important 22.7 (164) 23.6 (85) 21.8 (79)
Vivre_en_couple


0.6
    Important 19.8 (143) 21.4 (77) 18.3 (66)
    Pas du tout important 19.7 (142) 18.3 (66) 21.2 (77)
    Peu important 25.3 (182) 23.3 (84) 27.3 (99)
    Plus ou moins important 28.6 (206) 30.4 (109) 26.8 (97)
    Très important 6.5 (47) 6.7 (24) 6.4 (23)
Marier_1er_fois


0.5
    Important 9.2 (66) 9.6 (34) 8.8 (32)
    Pas du tout important 51.0 (368) 53.7 (193) 48.4 (175)
    Peu important 19.8 (143) 16.8 (60) 22.9 (83)
    Plus ou moins important 15.0 (108) 15.2 (55) 14.8 (53)
    Très important 4.9 (36) 4.7 (17) 5.2 (19)
Parent_1er_fois


0.7
    Important 16.5 (119) 16.7 (60) 16.3 (59)
    Pas du tout important 33.0 (238) 32.8 (118) 33.1 (120)
    Peu important 17.0 (122) 16.4 (59) 17.5 (63)
    Plus ou moins important 18.6 (134) 17.2 (62) 20.0 (72)
    Très important 14.9 (108) 16.9 (61) 13.0 (47)
1 % (n)
2 Pearson’s X^2: Rao & Scott adjustment

10. Marqueurs objectifs selon l’age

table_Marqueurs_objectifs_gpe_age <- 
  data_design %>%
  tbl_svysummary(
    by = gpe_age,
    include = c(Quiter_nid, Emploi_TPL, Vivre_en_couple, Marier_1er_fois, Parent_1er_fois, gpe_age),
  statistic = list(
    all_continuous() ~ "{mean}",
    all_categorical() ~ "{p} ({n})"
  ),
  digits=~ c(1,0),
  missing = "no"  
) %>%
  add_overall() %>%  
  add_p() %>%        
  modify_header(label = "**Caractéristiques**") %>% 
  modify_caption("**table_Marqueurs_objectifs_groupe**")

table_Marqueurs_objectifs_gpe_age
table_Marqueurs_objectifs_groupe
Caractéristiques Overall
N = 721
1
18-24 ans
N = 268
1
25-34 ans
N = 453
1
p-value2
Quiter_nid


0.5
    Important 44.0 (317) 43.4 (116) 44.4 (201)
    Pas du tout important 7.4 (53) 7.8 (21) 7.1 (32)
    Peu important 10.8 (78) 12.8 (34) 9.7 (44)
    Plus ou moins important 21.5 (155) 22.7 (61) 20.8 (94)
    Très important 16.3 (117) 13.4 (36) 18.0 (82)
Emploi_TPL


0.009
    Important 44.6 (321) 50.7 (136) 40.9 (185)
    Pas du tout important 2.8 (20) 3.0 (8) 2.8 (12)
    Peu important 7.8 (56) 9.8 (26) 6.6 (30)
    Plus ou moins important 22.1 (159) 21.2 (57) 22.6 (103)
    Très important 22.7 (164) 15.3 (41) 27.0 (122)
Vivre_en_couple


0.2
    Important 19.8 (143) 18.0 (48) 20.9 (95)
    Pas du tout important 19.7 (142) 20.4 (55) 19.3 (88)
    Peu important 25.3 (182) 29.3 (79) 22.9 (104)
    Plus ou moins important 28.6 (206) 24.7 (66) 30.9 (140)
    Très important 6.5 (47) 7.6 (20) 5.9 (27)
Marier_1er_fois


0.3
    Important 9.2 (66) 11.4 (31) 7.9 (36)
    Pas du tout important 51.0 (368) 45.8 (123) 54.1 (245)
    Peu important 19.8 (143) 21.8 (58) 18.7 (85)
    Plus ou moins important 15.0 (108) 16.2 (43) 14.3 (65)
    Très important 4.9 (36) 4.8 (13) 5.0 (23)
Parent_1er_fois


0.3
    Important 16.5 (119) 17.1 (46) 16.1 (73)
    Pas du tout important 33.0 (238) 28.6 (77) 35.6 (161)
    Peu important 17.0 (122) 18.2 (49) 16.2 (73)
    Plus ou moins important 18.6 (134) 21.8 (58) 16.7 (76)
    Très important 14.9 (108) 14.3 (38) 15.3 (69)
1 % (n)
2 Pearson’s X^2: Rao & Scott adjustment

#—————GRAPHIQUE1————————##########

11.Création du graphique pour l’ensemble de l’échantillon

# Regrouper les données et calculer les pourcentages
data_long <- data_design$variables %>%
  select(Quiter_nid, Emploi_TPL, Vivre_en_couple, Marier_1er_fois, Parent_1er_fois) %>%
  gather(key = "Marqueur_objectif", value = "Importance") %>%
  group_by(Marqueur_objectif, Importance) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Définir l'ordre des niveaux pour la variable "Importance"
levels_importance <- c("Pas du tout important", "Peu important", "Plus ou moins important", "Important", "Très important")
data_long$Importance <- factor(data_long$Importance, levels = levels_importance)

# Créer le graphique de barres empilées avec les pourcentages
a<-ggplot(data_long, aes(x = Marqueur_objectif, y = Percentage, fill = Importance)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), position = position_stack(vjust = 0.5), size = 3) +
  labs(x = "Marqueur_objectif", y = "Pourcentage", fill = "") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
   legend.position = "bottom")+
  coord_flip() +
  scale_fill_brewer(palette = "RdBu")


# Sauvegarder le graphique dans le sous-dossier 'figures/marqueur_objectif' avec le nom spécifié
ggsave(here("figures", "marqueur_objectif", "marqueur_objectif_pour-echantillion_plot.png"), plot = a, width = 10, height = 6)

#RdBu
#RdYlGn

a

#"BrBG"  

12. Création du graphique selon le sexe

# Supposons que data_P2 soit votre dataframe initial

# Regrouper les données par sexe et calculer les pourcentages
data_sexe <- data_design$variables %>%
  select(sexe, Quiter_nid, Emploi_TPL, Vivre_en_couple, Marier_1er_fois, Parent_1er_fois) %>%
  gather(key = "Marqueur", value = "Importance", -sexe) %>%
  group_by(sexe, Marqueur, Importance) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Définir l'ordre des niveaux pour la variable "Importance"
levels_importance <- c("Pas du tout important", "Peu important", "Plus ou moins important", "Important", "Très important")
data_sexe$Importance <- factor(data_sexe$Importance, levels = levels_importance)

# Créer le graphique de barres empilées avec les pourcentages par sexe
b<-ggplot(data_sexe, aes(x = Marqueur, y = Percentage, fill = Importance)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), position = position_stack(vjust = 0.5), size = 3) +
  facet_wrap(~sexe) +
  labs(x = "Marqueur_objectif", y = "Pourcentage", fill = "") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
   legend.position = "bottom") +
  coord_flip() +
  scale_fill_brewer(palette = "RdBu")


# Sauvegarder le graphique 
ggsave(here("figures", "marqueur_objectif", "marqueur_objectif_selon_sexe_plot.png"), plot = a, width = 10, height = 6)

b

13. Création du graphique selon l’âge

# Regrouper les données par sexe et calculer les pourcentages
data_gpe_age <- data_design$variables %>%
  select(gpe_age, Quiter_nid, Emploi_TPL, Vivre_en_couple, Marier_1er_fois, Parent_1er_fois) %>%
  gather(key = "Marqueur", value = "Importance", -gpe_age) %>%
  group_by(gpe_age, Marqueur, Importance) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Définir l'ordre des niveaux pour la variable "Importance"
levels_importance <- c("Pas du tout important", "Peu important", "Plus ou moins important", "Important", "Très important")
data_gpe_age$Importance <- factor(data_gpe_age$Importance, levels = levels_importance)

# Créer le graphique de barres empilées avec les pourcentages par sexe
c<-ggplot(data_gpe_age, aes(x = Marqueur, y = Percentage, fill = Importance)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), position = position_stack(vjust = 0.5), size = 3) +
  facet_wrap(~gpe_age) +
  labs(x = "Marqueur_objectif", y = "Pourcentage", fill = "") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
   legend.position = "bottom") +
  coord_flip() +
  scale_fill_brewer(palette = "RdBu")



# Sauvegarder le graphique dans le sous-dossier 'figures/marqueur_objectif' avec le nom spécifié
ggsave(here("figures", "marqueur_objectif", "marqueur_objectif_selon_age_plot.png"), plot = c, width = 10, height = 6)

c

14. Création du graphique selon le milieu de résidence

# Regrouper les données par sexe et calculer les pourcentages
data_Lieu_de_residence <- data_design$variables %>%
  select(Lieu_de_residence, Quiter_nid, Emploi_TPL, Vivre_en_couple, Marier_1er_fois, Parent_1er_fois) %>%
  gather(key = "Marqueur", value = "Importance", -Lieu_de_residence) %>%
  group_by(Lieu_de_residence, Marqueur, Importance) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Définir l'ordre des niveaux pour la variable "Importance"
levels_importance <- c("Pas du tout important", "Peu important", "Plus ou moins important", "Important", "Très important")
data_Lieu_de_residence$Importance <- factor(data_Lieu_de_residence$Importance, levels = levels_importance)

# Créer le graphique de barres empilées avec les pourcentages par sexe
d<-ggplot(data_Lieu_de_residence, aes(x = Marqueur, y = Percentage, fill = Importance)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), position = position_stack(vjust = 0.5), size = 3) +
  facet_wrap(~Lieu_de_residence) +
  labs(x = "Marqueur_objectif", y = "Pourcentage", fill = "") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
   legend.position = "bottom") +
  coord_flip() +
  scale_fill_brewer(palette = "RdBu")

# Sauvegarder le graphique dans le sous-dossier 'figures/marqueur_objectif' avec le nom spécifié

ggsave(here("figures", "marqueur_objectif", "marqueur_objectif_selon_milieu_residence_plot.png"), plot = d, width = 10, height = 6)

d

15. Création du graphique selon l’utilisation des médias sociaux

# Regrouper les données par sexe et calculer les pourcentages
data_Comptes_medias_sociaux <- data_design$variables %>%
  select(Usage_MS, Quiter_nid, Emploi_TPL, Vivre_en_couple, Marier_1er_fois, Parent_1er_fois) %>%
  gather(key = "Marqueur", value = "Importance", -Usage_MS) %>%
  group_by(Usage_MS, Marqueur, Importance) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Définir l'ordre des niveaux pour la variable "Importance"
levels_importance <- c("Pas du tout important", "Peu important", "Plus ou moins important", "Important", "Très important")
data_Comptes_medias_sociaux$Importance <- factor(data_Comptes_medias_sociaux$Importance, levels = levels_importance)

# Créer le graphique de barres empilées avec les pourcentages par sexe
e<-ggplot(data_Comptes_medias_sociaux, aes(x = Marqueur, y = Percentage, fill = Importance)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), position = position_stack(vjust = 0.5), size = 3) +
  facet_wrap(~Usage_MS) +
  labs(x = "Marqueur_objectif", y = "Pourcentage", fill = "") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
   legend.position = "bottom") +
  coord_flip() +
  scale_fill_brewer(palette = "RdBu")

# Sauvegarder le graphique dans le sous-dossier 'figures/marqueur_objectif' avec le nom spécifié
ggsave(here("figures", "marqueur_objectif", "marqueur_objectif_Utilisation_médias_plot.png"), plot = e, width = 10, height = 6)

e

. —————GrAPHIQUE2——————

#———————————————##############

16. Graphique de barres empilées pour l’ensemble de l’échantillon

# Création du graphique à barres avec pourcentages affichés
f <- ggplot(data_design$variables, aes(x = Somme_Importance, fill = Somme_Importance)) +
  geom_bar() +
  geom_text(stat = "count",
            aes(label = scales::percent(..count../sum(..count..), accuracy = 0.1) %>% gsub("%", "", .)),  # Enlève le symbole %
            vjust = -0.5) +
  labs(
    title = "",
    x = "Pourcentage",
    y = "Nombre d'individus"
  ) +
  scale_fill_brewer(palette = "RdBu") +
  theme_minimal() +
  theme(legend.position = "none")

# Sauvegarder le graphique dans le sous-dossier 'Somme_marqueur_objectif' avec le nom spécifié
ggsave(here("figures","marqueur_objectif", "_somme_importance_echantillon_plot.png"), plot = f, width = 10, height = 6)

f

17. selon l’age

# Regrouper les données par groupe d'âge et par modalité de Somme_Importance,
# et calculer le nombre d'individus et le pourcentage dans chaque groupe
data_gpe_age <- data_design$variables %>%
  group_by(gpe_age, Somme_Importance) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Définir l'ordre des niveaux pour la variable Somme_Importance
levels_importance <- c("0 oui (rien n’est important)", 
                         "1 oui", 
                         "2 oui", 
                         "3 oui", 
                         "4 oui", 
                         "5 oui (tout est important)")
data_gpe_age$Somme_Importance <- factor(data_gpe_age$Somme_Importance, levels = levels_importance)

# Création du graphique à barres empilées par groupe d'âge
i<-ggplot(data_gpe_age, aes(x = gpe_age, y = Percentage, fill = Somme_Importance)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), 
            position = position_stack(vjust = 0.5), size = 3) +
  labs(title = "Distribution de l'importance des dimensions par groupe d'âge",
       x = "Groupe d'âge", y = "Pourcentage", fill = "") +
  scale_y_continuous(labels = percent_format(scale = 1)) +
  scale_fill_brewer(palette = "PuBu") +
  theme_minimal() +
  theme(legend.position = "bottom")

# Sauvegarder le graphique dans le sous-dossier 'Somme_marqueur_objectif'
ggsave(here("figures","marqueur_objectif", "_somme_importance_selon_age_plot.png"), plot = i, width = 10, height = 6)

i

18. seloN le sexe

# Regrouper les données par groupe d'âge et par modalité de Somme_Importance,
# et calculer le nombre d'individus et le pourcentage dans chaque groupe
data_sexe <- data_design$variables %>%
  group_by(sexe, Somme_Importance) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()
## `summarise()` has grouped output by 'sexe'. You can override using the `.groups` argument.
# Définir l'ordre des niveaux pour la variable Somme_Importance
levels_importance <- c("0 oui (rien n’est important)", 
                         "1 oui", 
                         "2 oui", 
                         "3 oui", 
                         "4 oui", 
                         "5 oui (tout est important)")
data_sexe$Somme_Importance <- factor(data_sexe$Somme_Importance, levels = levels_importance)

# Création du graphique à barres empilées par groupe d'âge
j<-ggplot(data_sexe, aes(x = sexe, y = Percentage, fill = Somme_Importance)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), 
            position = position_stack(vjust = 0.5), size = 3) +
  labs(title = "",
       x = "Sexe", y = "Pourcentage", fill = "") +
  scale_y_continuous(labels = percent_format(scale = 1)) +
  scale_fill_brewer(palette = "PuBu") +
  theme_minimal()+
  theme(legend.position = "bottom")

# Sauvegarder le graphique dans le sous-dossier 'Somme_marqueur_objectif'
ggsave(here("figures","marqueur_objectif", "_somme_importance_selon_sexe_plot.png"), plot = j, width = 10, height = 6)

j

19. selon le milieu de residence

# Regrouper les données par groupe d'âge et par modalité de Somme_Importance,
# et calculer le nombre d'individus et le pourcentage dans chaque groupe
data_Lieu_de_residence <- data_design$variables %>%
  group_by(Lieu_de_residence, Somme_Importance) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Définir l'ordre des niveaux pour la variable Somme_Importance
levels_importance <- c("0 oui (rien n’est important)", 
                         "1 oui", 
                         "2 oui", 
                         "3 oui", 
                         "4 oui", 
                         "5 oui (tout est important)")
data_Lieu_de_residence$Somme_Importance <- factor(data_Lieu_de_residence$Somme_Importance, levels = levels_importance)

# Création du graphique à barres empilées par groupe d'âge
k<-ggplot(data_Lieu_de_residence, aes(x = Lieu_de_residence, y = Percentage, fill = Somme_Importance)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), 
            position = position_stack(vjust = 0.5), size = 3) +
  labs(title = "",
       x = "Lieu de residence", y = "Pourcentage", fill = "") +
  scale_y_continuous(labels = percent_format(scale = 1)) +
  scale_fill_brewer(palette = "PuBu") +
  theme_minimal() +
  theme(legend.position = "bottom")



# Sauvegarder le graphique dans le sous-dossier 'Somme_marqueur_objectif'
ggsave(here("figures","marqueur_objectif", "_somme_importance_selon_residence_plot.png"), plot = k, width = 10, height = 6)


k

20. Selon le utilisation des médias

# Regrouper les données par groupe d'âge et par modalité de Somme_Importance,
# et calculer le nombre d'individus et le pourcentage dans chaque groupe
data_Comptes_medias_sociaux <- data_design$variables %>%
  group_by(Usage_MS, Somme_Importance) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Définir l'ordre des niveaux pour la variable Somme_Importance
levels_importance <- c("0 oui (rien n’est important)", 
                         "1 oui", 
                         "2 oui", 
                         "3 oui", 
                         "4 oui", 
                         "5 oui (tout est important)")
data_Comptes_medias_sociaux$Somme_Importance <- factor(data_Comptes_medias_sociaux$Somme_Importance, levels = levels_importance)

# Création du graphique à barres empilées par groupe d'âge
l<-ggplot(data_Comptes_medias_sociaux, aes(x = Usage_MS, y = Percentage, fill = Somme_Importance)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), 
            position = position_stack(vjust = 0.5), size = 3) +
  labs(title = "",
       x = "", y = "Pourcentage", fill = "") +
  scale_y_continuous(labels = percent_format(scale = 1)) +
  scale_fill_brewer(palette = "PuBu") +
  theme_minimal() +
  theme(legend.position = "bottom")

# Sauvegarder le graphique dans le sous-dossier 'Somme_marqueur_objectif'
ggsave(here("figures","marqueur_objectif", "_somme_importance_médias_sociaux_plot.png"), plot = l, width = 10, height = 6)

l

21. Dindo

#install.packages(c("dplyr", "dendextend", "ade4", "ggdendro", "fastcluster", "FactoMineR"))

# Chargement des bibliothèques nécessaires
library(dplyr)
library(dendextend)
library(ade4)
library(ggdendro)
library(fastcluster)
source(url("https://raw.githubusercontent.com/larmarange/JLutils/master/R/clustering.R"))
#devtools::install_github("husson/FactoMineR")

#library(devtools)
#install_github("larmarange/JLutils")
#library(JLutils)
library(FactoMineR)

data_interest <- data_design$variables %>%
  select(Somme_Importance,sexe,gpe_age,Lieu_de_residence) %>%
  na.omit()

data_interest$Somme_Importance<-as.factor(data_interest$Somme_Importance)
data_interest$sexe<-as.factor(data_interest$sexe)
data_interest$gpe_age<-as.factor(data_interest$gpe_age)
data_interest$Lieu_de_residence<-as.factor(data_interest$Lieu_de_residence)

acm <- dudi.acm(data_interest, scannf = FALSE, nf = 5)

md <- dist.dudi(acm)

arbre <- hclust(md, method = "ward.D2")
#meilleure partition entre 3 et 20 classes.
best.cutree(arbre)
## [1] 6

#Affichage

ggdendrogram(arbre, labels = FALSE)

# Coloration des branches en fonction des clusters
library(ggplot2)
ggplot(color_branches(arbre, k = 6), labels = FALSE)

#Decoupage pour une analyse fine

typo <- cutree(arbre, 6)
freq(typo)
## Frequencies  
## typo  
## Type: Integer  
## 
##               Freq   % Valid   % Valid Cum.   % Total   % Total Cum.
## ----------- ------ --------- -------------- --------- --------------
##           1    155     21.44          21.44     21.44          21.44
##           2    147     20.33          41.77     20.33          41.77
##           3     54      7.47          49.24      7.47          49.24
##           4    142     19.64          68.88     19.64          68.88
##           5    197     27.25          96.13     27.25          96.13
##           6     28      3.87         100.00      3.87         100.00
##        <NA>      0                               0.00         100.00
##       Total    723    100.00         100.00    100.00         100.00
data_interest$typo<- cutree(arbre,6)

22. Caractériser la typologie

data_interest %>%
  tbl_summary(by = typo)
Characteristic 1
N = 155
1
2
N = 147
1
3
N = 54
1
4
N = 142
1
5
N = 197
1
6
N = 28
1
Somme_Importance





    0 oui (rien n’est important) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 28 (100%)
    1 oui 0 (0%) 0 (0%) 54 (100%) 0 (0%) 0 (0%) 0 (0%)
    2 oui 0 (0%) 0 (0%) 0 (0%) 0 (0%) 197 (100%) 0 (0%)
    3 oui 155 (100%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%)
    4 oui 0 (0%) 0 (0%) 0 (0%) 142 (100%) 0 (0%) 0 (0%)
    5 oui (tout est important) 0 (0%) 147 (100%) 0 (0%) 0 (0%) 0 (0%) 0 (0%)
sexe





    Femme 83 (54%) 93 (63%) 27 (50%) 90 (63%) 115 (58%) 15 (54%)
    Homme 72 (46%) 54 (37%) 27 (50%) 52 (37%) 82 (42%) 13 (46%)
gpe_age





    18-24 ans 72 (46%) 73 (50%) 26 (48%) 61 (43%) 93 (47%) 15 (54%)
    25-34 ans 83 (54%) 74 (50%) 28 (52%) 81 (57%) 104 (53%) 13 (46%)
Lieu_de_residence





    Ailleurs au Quebec 62 (40%) 60 (41%) 25 (46%) 56 (39%) 79 (40%) 15 (54%)
    Grand Motreal 93 (60%) 87 (59%) 29 (54%) 86 (61%) 118 (60%) 13 (46%)
1 n (%)

#classification avec les variables

#data_interest

23. CAH avec l’extension FactoMineR

data_interest <- data_design$variables %>%
  select(Somme_Importance,sexe,gpe_age,Lieu_de_residence) %>%
  na.omit()

data_interest$Somme_Importance<-as.factor(data_interest$Somme_Importance)
data_interest$sexe<-as.factor(data_interest$sexe)
data_interest$gpe_age<-as.factor(data_interest$gpe_age)
data_interest$Lieu_de_residence<-as.factor(data_interest$Lieu_de_residence)


acm2 <- MCA(data_interest[complete.cases(data_interest), ], ncp = 5, graph = FALSE)
cah <- HCPC(acm2, graph = FALSE)

#Affichage du cluster en 3 partitions

plot(cah, choice = "tree")

#affichage en 3D

plot(cah, choice = "3D.map")

#Affichage en en bar

plot(cah, choice = "bar")

#Affichage

plot(cah, choice = "map")

#cah$data.clust

#——##################—–Marqueurs subjectifs——####################——####### #——————————————————————————–

`

24. Marqueurs subjectifs selon le sexe

# Créer l'objet de design en tenant compte des pondérations


table_Marqueurs_subjectifs_sexe <- 
  data_design %>%
  tbl_svysummary(
    by = sexe,
    include = c(Propres_responsabilites, Propres_decisions, Independance_finance),
  statistic = list(
    all_continuous() ~ "{mean}",
    all_categorical() ~ "{p} ({n})"
  ),
  digits=~ c(1,0),
  missing = "no"  
) %>%
  add_overall() %>%  
  add_p() %>%        
  modify_header(label = "**Caractéristiques**") %>% 
  modify_caption("**table_Marqueurs_subjectifs_sexe**")

table_Marqueurs_subjectifs_sexe
table_Marqueurs_subjectifs_sexe
Caractéristiques Overall
N = 721
1
Femme
N = 359
1
Homme
N = 362
1
p-value2
Propres_responsabilites


<0.001
    Important 49.1 (354) 49.8 (179) 48.4 (175)
    Pas important 3.9 (28) 2.0 (7) 5.7 (21)
    Pas important du tout 1.9 (14) 0.3 (1) 3.4 (12)
    Plus ou moins important 9.6 (69) 6.8 (24) 12.4 (45)
    Très important 35.6 (256) 41.1 (147) 30.1 (109)
Propres_decisions


<0.001
    Important 43.6 (314) 45.6 (164) 41.5 (150)
    Pas important 3.1 (23) 1.8 (6) 4.5 (16)
    Pas important du tout 1.7 (13) 0.0 (0) 3.5 (13)
    Plus ou moins important 8.3 (59) 5.2 (19) 11.3 (41)
    Très important 43.3 (312) 47.5 (170) 39.2 (142)
Independance_finance


<0.001
    Important 44.3 (319) 47.8 (172) 40.8 (148)
    Pas important 3.8 (28) 2.0 (7) 5.7 (21)
    Pas important du tout 1.9 (14) 0.1 (0) 3.7 (13)
    Plus ou moins important 10.3 (75) 7.9 (28) 12.8 (46)
    Très important 39.6 (286) 42.2 (152) 37.0 (134)
1 % (n)
2 Pearson’s X^2: Rao & Scott adjustment

25. Marqueurs subjectifs selon l’age

table_Marqueurs_subjectifs_gpe_age <- 
  data_design %>%
  tbl_svysummary(
    by = gpe_age,
    include = c(Propres_responsabilites, Propres_decisions, Independance_finance),
  statistic = list(
    all_continuous() ~ "{mean}",
    all_categorical() ~ "{p} ({n})"
  ),
  digits=~ c(1,0),
  missing = "no"  
) %>%
  add_overall() %>%  
  add_p() %>%        
  modify_header(label = "**Caractéristiques**") %>% 
  modify_caption("**table_Marqueurs_subjectifs_gpe_age**")

table_Marqueurs_subjectifs_gpe_age
table_Marqueurs_subjectifs_gpe_age
Caractéristiques Overall
N = 721
1
18-24 ans
N = 268
1
25-34 ans
N = 453
1
p-value2
Propres_responsabilites


0.4
    Important 49.1 (354) 50.5 (135) 48.2 (218)
    Pas important 3.9 (28) 5.3 (14) 3.1 (14)
    Pas important du tout 1.9 (14) 0.9 (3) 2.4 (11)
    Plus ou moins important 9.6 (69) 10.2 (27) 9.2 (42)
    Très important 35.6 (256) 33.1 (89) 37.0 (168)
Propres_decisions


0.3
    Important 43.6 (314) 42.5 (114) 44.2 (200)
    Pas important 3.1 (23) 5.0 (13) 2.0 (9)
    Pas important du tout 1.7 (13) 2.2 (6) 1.5 (7)
    Plus ou moins important 8.3 (59) 9.3 (25) 7.7 (35)
    Très important 43.3 (312) 41.1 (110) 44.6 (202)
Independance_finance


0.2
    Important 44.3 (319) 42.6 (114) 45.3 (205)
    Pas important 3.8 (28) 6.4 (17) 2.3 (11)
    Pas important du tout 1.9 (14) 1.8 (5) 2.0 (9)
    Plus ou moins important 10.3 (75) 11.7 (31) 9.6 (43)
    Très important 39.6 (286) 37.5 (101) 40.9 (185)
1 % (n)
2 Pearson’s X^2: Rao & Scott adjustment

26. Marqueurs subjectifs selon le lieu de residence

table_Marqueurs_subjectifs_Lieu_de_residence <- 
  data_design %>%
  tbl_svysummary(
    by = Lieu_de_residence,
    include = c(Propres_responsabilites, Propres_decisions, Independance_finance),
  statistic = list(
    all_continuous() ~ "{mean}",
    all_categorical() ~ "{p} ({n})"
  ),
  digits=~ c(1,0),
  missing = "no"  
) %>%
  add_overall() %>%  
  add_p() %>%        
  modify_header(label = "**Caractéristiques**") %>% 
  modify_caption("**table_Marqueurs_subjectifs_Lieu_de_residence**")

table_Marqueurs_subjectifs_Lieu_de_residence
table_Marqueurs_subjectifs_Lieu_de_residence
Caractéristiques Overall
N = 721
1
Ailleurs au Quebec
N = 395
1
Grand Motreal
N = 326
1
p-value2
Propres_responsabilites


0.6
    Important 49.1 (354) 46.5 (183) 52.2 (170)
    Pas important 3.9 (28) 4.6 (18) 3.1 (10)
    Pas important du tout 1.9 (14) 2.2 (9) 1.4 (5)
    Plus ou moins important 9.6 (69) 9.5 (38) 9.6 (31)
    Très important 35.6 (256) 37.2 (147) 33.6 (110)
Propres_decisions


0.6
    Important 43.6 (314) 41.0 (162) 46.7 (152)
    Pas important 3.1 (23) 3.8 (15) 2.3 (7)
    Pas important du tout 1.7 (13) 2.0 (8) 1.4 (5)
    Plus ou moins important 8.3 (59) 8.2 (32) 8.3 (27)
    Très important 43.3 (312) 45.0 (178) 41.3 (135)
Independance_finance


0.5
    Important 44.3 (319) 41.3 (163) 47.9 (156)
    Pas important 3.8 (28) 4.3 (17) 3.3 (11)
    Pas important du tout 1.9 (14) 2.0 (8) 1.8 (6)
    Plus ou moins important 10.3 (75) 10.0 (39) 10.8 (35)
    Très important 39.6 (286) 42.4 (168) 36.2 (118)
1 % (n)
2 Pearson’s X^2: Rao & Scott adjustment

27. Marqueurs subjectifs selon l’utilisation des médias sociaux

table_Marqueurs_subjectifs_Comptes_medias_sociaux <- 
  data_design %>%
  tbl_svysummary(
    by = Usage_MS,
    include = c(Propres_responsabilites, Propres_decisions, Independance_finance),
  statistic = list(
    all_continuous() ~ "{mean}",
    all_categorical() ~ "{p} ({n})"
  ),
  digits=~ c(1,0),
  missing = "no"  
) %>%
  add_overall() %>%  
  add_p() %>%        
  modify_header(label = "**Caractéristiques**") %>% 
  modify_caption("**table_Marqueurs_subjectifs_Comptes_medias_sociaux**")

table_Marqueurs_subjectifs_Comptes_medias_sociaux
table_Marqueurs_subjectifs_Comptes_medias_sociaux
Caractéristiques Overall
N = 721
1
Non
N = 60
1
Oui
N = 660
1
p-value2
Propres_responsabilites


<0.001
    Important 49.1 (354) 26.6 (16) 51.1 (338)
    Pas important 3.9 (28) 20.1 (12) 2.4 (16)
    Pas important du tout 1.9 (14) 11.4 (7) 1.0 (7)
    Plus ou moins important 9.6 (69) 19.4 (12) 8.7 (57)
    Très important 35.6 (256) 22.5 (14) 36.8 (243)
Propres_decisions


<0.001
    Important 43.6 (314) 28.8 (17) 44.9 (297)
    Pas important 3.1 (23) 20.1 (12) 1.6 (10)
    Pas important du tout 1.7 (13) 8.4 (5) 1.1 (8)
    Plus ou moins important 8.3 (59) 26.9 (16) 6.5 (43)
    Très important 43.3 (312) 15.8 (10) 45.8 (303)
Independance_finance


<0.001
    Important 44.3 (319) 38.0 (23) 44.9 (296)
    Pas important 3.8 (28) 15.0 (9) 2.8 (19)
    Pas important du tout 1.9 (14) 13.2 (8) 0.9 (6)
    Plus ou moins important 10.3 (75) 18.2 (11) 9.6 (64)
    Très important 39.6 (286) 15.5 (9) 41.8 (276)
1 % (n)
2 Pearson’s X^2: Rao & Scott adjustment

#####Graphique marqueurs#####“”

28. ———-Echantillonnant—————–

library(ggplot2)
library(dplyr)
library(tidyr)
library(scales)

# Regrouper les données et calculer les pourcentages
data_long <- data_design$variables %>%
  select(Propres_responsabilites, Propres_decisions, Independance_finance) %>%
  gather(key = "Marqueur", value = "Importance") %>%
  group_by(Marqueur, Importance) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Définir l'ordre des niveaux pour la variable "Importance"
levels_importance <- c("Pas important du tout", "Pas important", "Plus ou moins important", "Important", "Très important")
data_long$Importance <- factor(data_long$Importance, levels = levels_importance)

# Créer le graphique à barres empilées horizontal avec affichage des pourcentages
n<-ggplot(data_long, aes(x = Marqueur, y = Percentage, fill = Importance)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), 
            position = position_stack(vjust = 0.5), size = 3) +
  labs(x = "Marqueur_subjectif", y = "Pourcentage", fill = "",
       title = "") +
  scale_y_continuous(labels = percent_format(scale = 1)) +
  scale_fill_brewer(palette = "RdBu") +
  theme_minimal() +
  theme(legend.position = "bottom")+
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  coord_flip()


ggsave(here("figures","marqueur_subjectif", "marqueurs_subjectif_echantillon_plot.png"), plot = n, width = 10, height = 6)


n

29. Création du graphique selon le sexe ,marqueurs subjectifs

# Regrouper les données par sexe et calculer les pourcentages
data_sexe <- data_design$variables %>%
  select(sexe,Propres_responsabilites, Propres_decisions, Independance_finance) %>%
  gather(key = "Marqueur", value = "Importance", -sexe) %>%
  group_by(sexe, Marqueur, Importance) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Définir l'ordre des niveaux pour la variable "Importance"
levels_importance <- c("Pas important du tout", "Pas important", "Plus ou moins important", "Important", "Très important")
data_sexe$Importance <- factor(data_sexe$Importance, levels = levels_importance)

# Créer le graphique de barres empilées avec les pourcentages par sexe
n1<-ggplot(data_sexe, aes(x = Marqueur, y = Percentage, fill = Importance)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), position = position_stack(vjust = 0.5), size = 3) +
  facet_wrap(~sexe) +
  labs(x = "Marqueur_subjectif", y = "Pourcentage", fill = "") +
  theme_minimal() +
  theme(legend.position = "bottom")+
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  coord_flip() +
  scale_fill_brewer(palette = "RdBu")


ggsave(here("figures","marqueur_subjectif", "marqueurs_subjectif_selon_sexe_plot.png"), plot = n1, width = 10, height = 6)


n1

30. Création du graphique selon l’age ,marqueurs subjectifs

# Regrouper les données par sexe et calculer les pourcentages
data_gpe_age <- data_design$variables %>%
  select(gpe_age,Propres_responsabilites, Propres_decisions, Independance_finance) %>%
  gather(key = "Marqueur", value = "Importance", -gpe_age) %>%
  group_by(gpe_age, Marqueur, Importance) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Définir l'ordre des niveaux pour la variable "Importance"
levels_importance <- c("Pas important du tout", "Pas important", "Plus ou moins important", "Important", "Très important")
data_gpe_age$Importance <- factor(data_gpe_age$Importance, levels = levels_importance)

# Créer le graphique de barres empilées avec les pourcentages par sexe
n2<-ggplot(data_gpe_age, aes(x = Marqueur, y = Percentage, fill = Importance)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), position = position_stack(vjust = 0.5), size = 3) +
  facet_wrap(~gpe_age) +
  labs(x = "Marqueur_subjectif", y = "Pourcentage", fill = "") +
  theme_minimal() + 
  theme(legend.position = "bottom")+
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  coord_flip() +
  scale_fill_brewer(palette = "RdBu")

ggsave(here("figures", "marqueur_subjectif", "marqueurs_subjectif_selon_age_plot.png"), plot = n2, width = 10, height = 6)


n2

31. Création du graphique selon le lieu de residence ,marqueurs subjectifs

# Regrouper les données selon le lieu de residence et calculer les pourcentages
data_Lieu_de_residence <- data_design$variables %>%
  select(Lieu_de_residence,Propres_responsabilites, Propres_decisions, Independance_finance) %>%
  gather(key = "Marqueur", value = "Importance", -Lieu_de_residence) %>%
  group_by(Lieu_de_residence, Marqueur, Importance) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Définir l'ordre des niveaux pour la variable "Importance"
levels_importance <- c("Pas important du tout", "Pas important", "Plus ou moins important", "Important", "Très important")
data_Lieu_de_residence$Importance <- factor(data_Lieu_de_residence$Importance, levels = levels_importance)

# Créer le graphique de barres empilées avec les pourcentages par sexe
n3<-ggplot(data_Lieu_de_residence, aes(x = Marqueur, y = Percentage, fill = Importance)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), position = position_stack(vjust = 0.5), size = 3) +
  facet_wrap(~Lieu_de_residence) +
  labs(x = "Marqueur_subjectif", y = "Pourcentage", fill = "") +
  theme_minimal() +
  theme(legend.position = "bottom")+
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  coord_flip() +
  scale_fill_brewer(palette = "RdBu")


ggsave(here("figures", "marqueur_subjectif", "marqueurs_subjectif_selon_milieu_residence_plot.png"), plot = n3, width = 10, height = 6)


n3

32. Création du graphique selon l’utilisation des médias sociaux ,marqueurs subjectifs

# Regrouper les données  et calculer les pourcentages
data_Comptes_medias_sociaux <- data_design$variables %>%
  select(Usage_MS,Propres_responsabilites, Propres_decisions, Independance_finance) %>%
  gather(key = "Marqueur", value = "Importance", -Usage_MS) %>%
  group_by(Usage_MS, Marqueur, Importance) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Définir l'ordre des niveaux pour la variable "Importance"
levels_importance <- c("Pas important du tout", "Pas important", "Plus ou moins important", "Important", "Très important")
data_Comptes_medias_sociaux$Importance <- factor(data_Comptes_medias_sociaux$Importance, levels = levels_importance)

# Créer le graphique de barres empilées avec les pourcentages par usage des méadias
n4<-ggplot(data_Comptes_medias_sociaux, aes(x = Marqueur, y = Percentage, fill = Importance)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), position = position_stack(vjust = 0.5), size = 3) +
  facet_wrap(~Usage_MS) +
  labs(x = "Marqueur_subjectif", y = "Pourcentage", fill = "") +
  theme_minimal() + 
  theme(legend.position = "bottom")+
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  coord_flip() +
  scale_fill_brewer(palette = "RdBu")

ggsave(here("figures", "marqueur_subjectif", "marqueurs_subjectif_utilisation_médias_plot.png"), plot = n4, width = 10, height = 6)


n4

33. Creation de la nouvelle variable

#————-Graphique pour l’echantillon———-########” #————————-““““““““““““—————————————-

# Création du graphique à barres avec pourcentages affichés
o<-ggplot(data_design, aes(x = Somme_Importance_subjectif, fill = Somme_Importance_subjectif)) +
  geom_bar() +
  geom_text(stat = "count",
            aes(label = scales::percent(..count../sum(..count..), accuracy = 0.1) %>% gsub("%", "", .)),
            vjust = -0.5) +
  labs(
    title = "Distribution de l'importance des dimensions",
    x = "Nombre de dimensions considérées comme importantes",
    y = "Nombre d'individus"
  ) +
  scale_fill_brewer(palette = "PuBu") +
  theme_minimal() +
  theme(legend.position = "none")


ggsave(here("figures", "marqueur_subjectif", "Somme_subjectif_echantillon_plot.png"), plot = o, width = 10, height = 6)

o

35.selon l’age

# Regrouper les données par groupe d'âge et par modalité de Somme_Importance,
# et calculer le nombre d'individus et le pourcentage dans chaque groupe
data_gpe_age <- data_design$variables %>%
  group_by(gpe_age, Somme_Importance_subjectif) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Définir l'ordre des niveaux pour la variable Somme_Importance
levels_importance <- c("0 oui (rien n’est important)", 
                         "1 oui", 
                         "2 oui", 
                         "3 oui (tout est important)")
data_gpe_age$Somme_Importance_subjectif <- factor(data_gpe_age$Somme_Importance_subjectif, levels = levels_importance)

# Création du graphique à barres empilées par groupe d'âge
o1<-ggplot(data_gpe_age, aes(x = gpe_age, y = Percentage, fill = Somme_Importance_subjectif)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), 
            position = position_stack(vjust = 0.5), size = 3) +
  labs(title = "",
       x = "Groupe d'âge", y = "Pourcentage", fill = "") +
  scale_y_continuous(labels = percent_format(scale = 1)) +
  scale_fill_brewer(palette = "PuBu") +
  theme_minimal()+
  theme(legend.position = "bottom")


ggsave(here("figures", "marqueur_subjectif", "Somme_subjectif_selon_age_plot.png"), plot = o1, width = 10, height = 6)


o1

36. selon le sexe

 #Regrouper les données selon le sexe et par modalité de Somme_Importance,
# et calculer le nombre d'individus et le pourcentage dans chaque groupe
data_sexe <- data_design$variables %>%
  group_by(sexe, Somme_Importance_subjectif) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Définir l'ordre des niveaux pour la variable Somme_Importance
levels_importance <- c("0 oui (rien n’est important)", 
                         "1 oui", 
                         "2 oui", 
                         "3 oui (tout est important)")
data_sexe$Somme_Importance_subjectif <- factor(data_sexe$Somme_Importance_subjectif, levels = levels_importance)

# Création du graphique à barres empilées par groupe d'âge
o2<-ggplot(data_sexe, aes(x = sexe, y = Percentage, fill = Somme_Importance_subjectif)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), 
            position = position_stack(vjust = 0.5), size = 3) +
  labs(title = "",
       x = "Sexe", y = "Pourcentage", fill = "") +
  scale_y_continuous(labels = percent_format(scale = 1)) +
  scale_fill_brewer(palette = "PuBu") +
  theme_minimal()+
  theme(legend.position = "bottom")


ggsave(here("figures", "marqueur_subjectif", "Somme_subjectif_selon_sexe_plot.png"), plot = o2, width = 10, height = 6)


o2

37. selon le milieu de residence

 #Regrouper les données  selon le milieu de residence et par modalité de Somme_Importance,
# et calculer le nombre d'individus et le pourcentage dans chaque groupe
data_Lieu_de_residence <- data_design$variables %>%
  group_by(Lieu_de_residence, Somme_Importance_subjectif) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Définir l'ordre des niveaux pour la variable Somme_Importance
levels_importance <- c("0 oui (rien n’est important)", 
                         "1 oui", 
                         "2 oui", 
                         "3 oui (tout est important)")
data_Lieu_de_residence$Somme_Importance_subjectif <- factor(data_Lieu_de_residence$Somme_Importance_subjectif, levels = levels_importance)

# Création du graphique à barres empilées par groupe d'âge
o3<-ggplot(data_Lieu_de_residence, aes(x = Lieu_de_residence, y = Percentage, fill = Somme_Importance_subjectif)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), 
            position = position_stack(vjust = 0.5), size = 3) +
  labs(title = "",
       x = "Lieu de residence", y = "Pourcentage", fill = "") +
  scale_y_continuous(labels = percent_format(scale = 1)) +
  scale_fill_brewer(palette = "PuBu") +
  theme_minimal()+
  theme(legend.position = "bottom")


ggsave(here("figures", "marqueur_subjectif", "Somme_subjectif_lieu_de_residence_plot.png"), plot = o3, width = 10, height = 6)


o3

38. selon l’utilisation des médias

 #Regrouper les données par groupe d'âge et par modalité de Somme_Importance,
# et calculer le nombre d'individus et le pourcentage dans chaque groupe
data_Comptes_medias_sociaux <- data_design$variables %>%
  group_by(Usage_MS, Somme_Importance_subjectif) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()
## `summarise()` has grouped output by 'Usage_MS'. You can override using the `.groups`
## argument.
# Définir l'ordre des niveaux pour la variable Somme_Importance
levels_importance <- c("0 oui (rien n’est important)", 
                         "1 oui", 
                         "2 oui", 
                         "3 oui (tout est important)")
data_Comptes_medias_sociaux$Somme_Importance_subjectif <- factor(data_Comptes_medias_sociaux$Somme_Importance_subjectif, levels = levels_importance)

# Création du graphique à barres empilées par groupe d'âge
o4<-ggplot(data_Comptes_medias_sociaux, aes(x = Usage_MS, y = Percentage, fill = Somme_Importance_subjectif)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), 
            position = position_stack(vjust = 0.5), size = 3) +
  labs(title = "Distribution de l'importance des dimensions selon l'utilisation des médias sociaux",
       x = "Usage_medias_sociaux", y = "Pourcentage", fill = "") +
  scale_y_continuous(labels = percent_format(scale = 1)) +
  scale_fill_brewer(palette = "PuBu") +
  theme_minimal()+
  theme(legend.position = "bottom")



ggsave(here("figures", "marqueur_subjectif", "Somme_subjectif_utilisation_medias_plot.png"), plot = o4, width = 10, height = 6)


o4

39. Dindogramme variable subjectifs

# Chargement des bibliothèques nécessaires
library(dplyr)
library(dendextend)
library(ade4)
library(ggdendro)
library(fastcluster)
#library(devtools)
#install_github("larmarange/JLutils")
#library(JLutils)
source(url("https://raw.githubusercontent.com/larmarange/JLutils/master/R/clustering.R"))


data_interest<-0


# Sélectionner les variables d'intérêt et supprimer les lignes avec des valeurs manquantes
data_interest <- data_design$variables %>%
  select(Somme_Importance_subjectif, sexe, gpe_age, Lieu_de_residence) %>%
  na.omit()

# Convertir les variables en facteurs
data_interest <- data_interest %>%
  mutate(across(everything(), as.factor))

# Supprimer les niveaux de facteurs sans occurrences
data_interest <- data_interest %>%
  mutate(across(everything(), ~ fct_drop(.)))

# Vérifier la distribution des catégories pour chaque variable
summary(data_interest)
##                 Somme_Importance_subjectif    sexe          gpe_age   
##  0 oui (rien n’est important):  6          Femme:423   18-24 ans:340  
##  1 oui                       : 21          Homme:300   25-34 ans:383  
##  2 oui                       : 37                                     
##  3 oui (tout est important)  :659                                     
##           Lieu_de_residence
##  Ailleurs au Quebec:297    
##  Grand Motreal     :426    
##                            
## 
# Effectuer l'ACM
acm <- dudi.acm(data_interest, scannf = FALSE, nf = 5)

# Calculer la matrice de distance
md <- dist.dudi(acm)

# Construire le dendrogramme en utilisant la méthode de Ward
arbre <- hclust(md, method = "ward.D2")

#meilleure partition entre 3 et 20 classes.

#meilleure partition entre 3 et 20 classes.
best.cutree(arbre)
## [1] 10

ggdendrogram(arbre, labels = FALSE)

# Coloration des branches en fonction des clusters
library(ggplot2)
ggplot(color_branches(arbre, k = 10), labels = FALSE)

typo <- cutree(arbre, 10)
freq(typo)
## Frequencies  
## typo  
## Type: Integer  
## 
##               Freq   % Valid   % Valid Cum.   % Total   % Total Cum.
## ----------- ------ --------- -------------- --------- --------------
##           1    109     15.08          15.08     15.08          15.08
##           2     78     10.79          25.86     10.79          25.86
##           3    120     16.60          42.46     16.60          42.46
##           4    130     17.98          60.44     17.98          60.44
##           5      6      0.83          61.27      0.83          61.27
##           6     70      9.68          70.95      9.68          70.95
##           7     85     11.76          82.71     11.76          82.71
##           8     37      5.12          87.83      5.12          87.83
##           9     21      2.90          90.73      2.90          90.73
##          10     67      9.27         100.00      9.27         100.00
##        <NA>      0                               0.00         100.00
##       Total    723    100.00         100.00    100.00         100.00
data_interest$typo<- cutree(arbre,10)
data_interest %>%
  tbl_summary(by = typo)
Characteristic 1
N = 109
1
2
N = 78
1
3
N = 120
1
4
N = 130
1
5
N = 6
1
6
N = 70
1
7
N = 85
1
8
N = 37
1
9
N = 21
1
10
N = 67
1
Somme_Importance_subjectif









    0 oui (rien n’est important) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 6 (100%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%)
    1 oui 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 21 (100%) 0 (0%)
    2 oui 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 37 (100%) 0 (0%) 0 (0%)
    3 oui (tout est important) 109 (100%) 78 (100%) 120 (100%) 130 (100%) 0 (0%) 70 (100%) 85 (100%) 0 (0%) 0 (0%) 67 (100%)
sexe









    Femme 0 (0%) 0 (0%) 120 (100%) 130 (100%) 0 (0%) 70 (100%) 85 (100%) 11 (30%) 7 (33%) 0 (0%)
    Homme 109 (100%) 78 (100%) 0 (0%) 0 (0%) 6 (100%) 0 (0%) 0 (0%) 26 (70%) 14 (67%) 67 (100%)
gpe_age









    18-24 ans 37 (34%) 0 (0%) 0 (0%) 130 (100%) 1 (17%) 70 (100%) 0 (0%) 20 (54%) 15 (71%) 67 (100%)
    25-34 ans 72 (66%) 78 (100%) 120 (100%) 0 (0%) 5 (83%) 0 (0%) 85 (100%) 17 (46%) 6 (29%) 0 (0%)
Lieu_de_residence









    Ailleurs au Quebec 109 (100%) 0 (0%) 0 (0%) 0 (0%) 4 (67%) 70 (100%) 85 (100%) 18 (49%) 11 (52%) 0 (0%)
    Grand Motreal 0 (0%) 78 (100%) 120 (100%) 130 (100%) 2 (33%) 0 (0%) 0 (0%) 19 (51%) 10 (48%) 67 (100%)
1 n (%)
#data_interest

40. FactoMineR

library(FactoMineR)

data_interest<-0
data_interest <- data_design$variables %>%
  select(Somme_Importance_subjectif,sexe,gpe_age,Lieu_de_residence) %>%
  na.omit()

data_interest$Somme_Importance<-as.factor(data_interest$Somme_Importance_subjectif)
data_interest$sexe<-as.factor(data_interest$sexe)
data_interest$gpe_age<-as.factor(data_interest$gpe_age)
data_interest$Lieu_de_residence<-as.factor(data_interest$Lieu_de_residence)


acm2 <- MCA(data_interest[complete.cases(data_interest), ], ncp = 5, graph = FALSE)
cah <- HCPC(acm2, graph = FALSE)

#Affichage du cluster en 3 partitions

plot(cah, choice = "tree")

#affichage en 3D

plot(cah, choice = "3D.map")

#Affichage en en bar

plot(cah, choice = "bar")

#Affichage

plot(cah, choice = "map")

#cah$data.clust

#Les deux variables en meme temps

41. Graphique pour les deux marqueurs

# Préparation des données au format long
data_long <- data_design$variables %>%
  select(Marq_objectif, Marq_subjectif) %>%
  pivot_longer(cols = everything(), names_to = "Marqueur", values_to = "Importance") %>%
  group_by(Marqueur, Importance) %>%
  summarise(Count = n(), .groups = 'drop') %>%
  group_by(Marqueur) %>%
  mutate(Percentage = Count / sum(Count) * 100) %>%
  ungroup()

# Création du graphique à barres empilées
graphique <- ggplot(data_long, aes(x = Marqueur, y = Percentage, fill = as.factor(Importance))) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.1f", Percentage)), 
            position = position_stack(vjust = 0.5), size = 3) +
  labs(x = "", y = "Pourcentage", fill = "",
       title = "Comparaison des marqueurs objectif et subjectif") +
  scale_y_continuous(labels = percent_format(scale = 1)) +
  scale_fill_brewer(palette = "PuBu") +
  theme_minimal() +
  theme(legend.position = "bottom",
        axis.text.x = element_text(angle = 45, hjust = 1))

# Sauvegarde du graphique
ggsave(here("figures", "marqueur_Objectif_subjectif", "Somme_subjectif_objecti_echantillon_plot.png"), 
       plot = graphique, width = 10, height = 6)


# Affichage du graphique
graphique

42.Dingogramme

library(dplyr)
library(dendextend)
library(ade4)
library(ggdendro)
library(fastcluster)
#library(devtools)
#install_github("larmarange/JLutils")
#library(JLutils)
source(url("https://raw.githubusercontent.com/larmarange/JLutils/master/R/clustering.R"))

data_interest<-0
data_interest <- data_design$variables %>%
  select(Somme_Importance,Somme_Importance_subjectif,gpe_age) %>%
  na.omit()

# Convertir les variables en facteurs
data_interest <- data_interest %>%
  mutate(across(everything(), as.factor))



# Supprimer les niveaux de facteurs sans occurrences
data_interest <- data_interest %>%
  mutate(across(everything(), ~ fct_drop(.)))

# Suppression des niveaux inutilisés de la variable Somme_Importance_subjectif
data_interest$Somme_Importance_subjectif <- fct_drop(data_interest$Somme_Importance_subjectif)

# Vérifier la distribution des catégories pour chaque variable
summary(data_interest)
##                      Somme_Importance                Somme_Importance_subjectif
##  0 oui (rien n’est important): 28     0 oui (rien n’est important):  6         
##  1 oui                       : 54     1 oui                       : 21         
##  2 oui                       :197     2 oui                       : 37         
##  3 oui                       :155     3 oui (tout est important)  :659         
##  4 oui                       :142                                              
##  5 oui (tout est important)  :147                                              
##       gpe_age   
##  18-24 ans:340  
##  25-34 ans:383  
##                 
##                 
##                 
## 
acm <- dudi.acm(data_interest, scannf = FALSE, nf = 5)

md <- dist.dudi(acm)

arbre <- hclust(md, method = "ward.D2")

#meilleure partition entre 3 et 20 classes.

#meilleure partition entre 3 et 20 classes.
best.cutree(arbre)
## [1] 9

ggdendrogram(arbre, labels = FALSE)

# Coloration des branches en fonction des clusters
library(ggplot2)
ggplot(color_branches(arbre, k = 9), labels = FALSE)

typo <- cutree(arbre, 9)
freq(typo)
## Frequencies  
## typo  
## Type: Integer  
## 
##               Freq   % Valid   % Valid Cum.   % Total   % Total Cum.
## ----------- ------ --------- -------------- --------- --------------
##           1    141     19.50          19.50     19.50          19.50
##           2    138     19.09          38.59     19.09          38.59
##           3     48      6.64          45.23      6.64          45.23
##           4    134     18.53          63.76     18.53          63.76
##           5    180     24.90          88.66     24.90          88.66
##           6      6      0.83          89.49      0.83          89.49
##           7     23      3.18          92.67      3.18          92.67
##           8     21      2.90          95.57      2.90          95.57
##           9     32      4.43         100.00      4.43         100.00
##        <NA>      0                               0.00         100.00
##       Total    723    100.00         100.00    100.00         100.00
data_interest$typo<- cutree(arbre,9)
data_interest %>%
  tbl_summary(by = typo)
Characteristic 1
N = 141
1
2
N = 138
1
3
N = 48
1
4
N = 134
1
5
N = 180
1
6
N = 6
1
7
N = 23
1
8
N = 21
1
9
N = 32
1
Somme_Importance








    0 oui (rien n’est important) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 2 (33%) 23 (100%) 3 (14%) 0 (0%)
    1 oui 0 (0%) 0 (0%) 48 (100%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 3 (14%) 3 (9.4%)
    2 oui 0 (0%) 0 (0%) 0 (0%) 0 (0%) 180 (100%) 2 (33%) 0 (0%) 7 (33%) 8 (25%)
    3 oui 141 (100%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 4 (19%) 10 (31%)
    4 oui 0 (0%) 0 (0%) 0 (0%) 134 (100%) 0 (0%) 2 (33%) 0 (0%) 1 (4.8%) 5 (16%)
    5 oui (tout est important) 0 (0%) 138 (100%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 3 (14%) 6 (19%)
Somme_Importance_subjectif








    0 oui (rien n’est important) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 6 (100%) 0 (0%) 0 (0%) 0 (0%)
    1 oui 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 21 (100%) 0 (0%)
    2 oui 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 5 (22%) 0 (0%) 32 (100%)
    3 oui (tout est important) 141 (100%) 138 (100%) 48 (100%) 134 (100%) 180 (100%) 0 (0%) 18 (78%) 0 (0%) 0 (0%)
gpe_age








    18-24 ans 64 (45%) 66 (48%) 23 (48%) 59 (44%) 81 (45%) 1 (17%) 14 (61%) 15 (71%) 17 (53%)
    25-34 ans 77 (55%) 72 (52%) 25 (52%) 75 (56%) 99 (55%) 5 (83%) 9 (39%) 6 (29%) 15 (47%)
1 n (%)
#data_interest

`

#I. Regression

1. Fonction pour exécuter la régression logistique

#install.packages(c("dplyr", "broom", "ggplot2", "purrr", "forestmodel"))

library(dplyr)
library(broom)
library(ggplot2)
library(purrr)
library(forestmodel)


# Fonction pour exécuter la régression logistique et extraire les résultats
run_logistic_regression <- function(dep_var, indep_vars, model_name, data) {
  formula <- as.formula(paste(dep_var, "~", paste(indep_vars, collapse = " + ")))
  model <- glm(formula, data = data, family = binomial)
  
  tidy(model, exponentiate = TRUE, conf.int = TRUE) %>%
    mutate(Model = model_name, Dependent = dep_var) %>%
    select(term, estimate, conf.low, conf.high, Model, Dependent)
}

3 .Forest avec forest_model

# Définition des variables dépendantes et indépendantes
dependent_vars <- c("Quiter_nid_rec", "Emploi_TPL_rec", "Vivre_en_couple_rec", 
                    "Marier_1er_fois_rec", "Parent_1er_fois_rec", 
                    "Propres_responsabilites_rec", "Propres_decisions_rec", 
                    "Independance_finance_rec")

# Liste des modèles logistiques par variable dépendante
models <- lapply(dependent_vars, function(dep_var) {
  list(
    "Modèle 1a" = glm(as.formula(paste(dep_var, "~ Nbr_compte_MS")), data = data_P3_, family = binomial),
    "Modèle 1b" = glm(as.formula(paste(dep_var, "~ Nbr_compte_MS_regulier")), data = data_P3_, family = binomial),
    "Modèle 1c" = glm(as.formula(paste(dep_var, "~ Nbr_compte_MS + Nbr_compte_MS_regulier")), data = data_P3_, family = binomial),
    "Modèle 2"  = glm(as.formula(paste(dep_var, "~ Nbr_compte_MS + Nbr_compte_MS_regulier + age + sexe + Lieu_de_residence")), data = data_P3_, family = binomial),
    "Modèle 3"  = glm(as.formula(paste(dep_var, "~ Nbr_compte_MS + Nbr_compte_MS_regulier + age + sexe + Lieu_de_residence + Diplome_du_repondant")), data = data_P3_, family = binomial)
  )
})

# Appliquer forest_model à chaque modèle en ajoutant un titre clair
lapply(seq_along(models), function(i) {
  dep_var <- dependent_vars[i]
  lapply(names(models[[i]]), function(model_name) {
    cat("\n--- Forest Plot pour", dep_var, "-", model_name, "---\n")  # Ajouter un titre clair
    print(forest_model(models[[i]][[model_name]]))  # Générer le plot
  })
})
## 
## --- Forest Plot pour Quiter_nid_rec - Modèle 1a ---

## 
## --- Forest Plot pour Quiter_nid_rec - Modèle 1b ---

## 
## --- Forest Plot pour Quiter_nid_rec - Modèle 1c ---

## 
## --- Forest Plot pour Quiter_nid_rec - Modèle 2 ---

## 
## --- Forest Plot pour Quiter_nid_rec - Modèle 3 ---

## 
## --- Forest Plot pour Emploi_TPL_rec - Modèle 1a ---

## 
## --- Forest Plot pour Emploi_TPL_rec - Modèle 1b ---

## 
## --- Forest Plot pour Emploi_TPL_rec - Modèle 1c ---

## 
## --- Forest Plot pour Emploi_TPL_rec - Modèle 2 ---

## 
## --- Forest Plot pour Emploi_TPL_rec - Modèle 3 ---

## 
## --- Forest Plot pour Vivre_en_couple_rec - Modèle 1a ---

## 
## --- Forest Plot pour Vivre_en_couple_rec - Modèle 1b ---

## 
## --- Forest Plot pour Vivre_en_couple_rec - Modèle 1c ---

## 
## --- Forest Plot pour Vivre_en_couple_rec - Modèle 2 ---

## 
## --- Forest Plot pour Vivre_en_couple_rec - Modèle 3 ---

## 
## --- Forest Plot pour Marier_1er_fois_rec - Modèle 1a ---

## 
## --- Forest Plot pour Marier_1er_fois_rec - Modèle 1b ---

## 
## --- Forest Plot pour Marier_1er_fois_rec - Modèle 1c ---

## 
## --- Forest Plot pour Marier_1er_fois_rec - Modèle 2 ---

## 
## --- Forest Plot pour Marier_1er_fois_rec - Modèle 3 ---

## 
## --- Forest Plot pour Parent_1er_fois_rec - Modèle 1a ---

## 
## --- Forest Plot pour Parent_1er_fois_rec - Modèle 1b ---

## 
## --- Forest Plot pour Parent_1er_fois_rec - Modèle 1c ---

## 
## --- Forest Plot pour Parent_1er_fois_rec - Modèle 2 ---

## 
## --- Forest Plot pour Parent_1er_fois_rec - Modèle 3 ---

## 
## --- Forest Plot pour Propres_responsabilites_rec - Modèle 1a ---

## 
## --- Forest Plot pour Propres_responsabilites_rec - Modèle 1b ---

## 
## --- Forest Plot pour Propres_responsabilites_rec - Modèle 1c ---

## 
## --- Forest Plot pour Propres_responsabilites_rec - Modèle 2 ---

## 
## --- Forest Plot pour Propres_responsabilites_rec - Modèle 3 ---

## 
## --- Forest Plot pour Propres_decisions_rec - Modèle 1a ---

## 
## --- Forest Plot pour Propres_decisions_rec - Modèle 1b ---

## 
## --- Forest Plot pour Propres_decisions_rec - Modèle 1c ---

## 
## --- Forest Plot pour Propres_decisions_rec - Modèle 2 ---

## 
## --- Forest Plot pour Propres_decisions_rec - Modèle 3 ---

## 
## --- Forest Plot pour Independance_finance_rec - Modèle 1a ---

## 
## --- Forest Plot pour Independance_finance_rec - Modèle 1b ---

## 
## --- Forest Plot pour Independance_finance_rec - Modèle 1c ---

## 
## --- Forest Plot pour Independance_finance_rec - Modèle 2 ---

## 
## --- Forest Plot pour Independance_finance_rec - Modèle 3 ---

## [[1]]
## [[1]][[1]]

## 
## [[1]][[2]]

## 
## [[1]][[3]]

## 
## [[1]][[4]]

## 
## [[1]][[5]]

## 
## 
## [[2]]
## [[2]][[1]]

## 
## [[2]][[2]]

## 
## [[2]][[3]]

## 
## [[2]][[4]]

## 
## [[2]][[5]]

## 
## 
## [[3]]
## [[3]][[1]]

## 
## [[3]][[2]]

## 
## [[3]][[3]]

## 
## [[3]][[4]]

## 
## [[3]][[5]]

## 
## 
## [[4]]
## [[4]][[1]]

## 
## [[4]][[2]]

## 
## [[4]][[3]]

## 
## [[4]][[4]]

## 
## [[4]][[5]]

## 
## 
## [[5]]
## [[5]][[1]]

## 
## [[5]][[2]]

## 
## [[5]][[3]]

## 
## [[5]][[4]]

## 
## [[5]][[5]]

## 
## 
## [[6]]
## [[6]][[1]]

## 
## [[6]][[2]]

## 
## [[6]][[3]]

## 
## [[6]][[4]]

## 
## [[6]][[5]]

## 
## 
## [[7]]
## [[7]][[1]]

## 
## [[7]][[2]]

## 
## [[7]][[3]]

## 
## [[7]][[4]]

## 
## [[7]][[5]]

## 
## 
## [[8]]
## [[8]][[1]]

## 
## [[8]][[2]]

## 
## [[8]][[3]]

## 
## [[8]][[4]]

## 
## [[8]][[5]]

4. Regroupement des regression

#install.packages(c("dplyr", "broom", "purrr", "stargazer"))

# Charger les bibliothèques nécessaires
library(dplyr)
library(broom)
library(purrr)
library(stargazer)

# Fonction pour exécuter une régression logistique et retourner le modèle
run_logistic_regression <- function(dep_var, indep_vars, data) {
  formula <- as.formula(paste(dep_var, "~", paste(indep_vars, collapse = " + ")))
  model <- glm(formula, data = data, family = binomial)
  return(model)  # Retourne le modèle au lieu des résultats sous forme de tableau
}

# Définition des variables dépendantes et indépendantes
dependent_vars <- c("Quiter_nid_rec", "Emploi_TPL_rec", "Vivre_en_couple_rec", 
                    "Marier_1er_fois_rec", "Parent_1er_fois_rec", 
                    "Propres_responsabilites_rec", "Propres_decisions_rec", 
                    "Independance_finance_rec")

indep_vars_list <- list(
  "Modèle 1a" = c("Usage_MS"),
  "Modèle 1b" = c("Nbr_compte_MS"),
  "Modèle 1c" = c("Nbr_compte_MS_regulier"),
  "Modèle 1d" = c("Usage_MS", "Nbr_compte_MS", "Nbr_compte_MS_regulier")
)

# Exécuter toutes les régressions et stocker les modèles
models <- list()

for (dep_var in dependent_vars) {
  for (model_name in names(indep_vars_list)) {
    model <- run_logistic_regression(dep_var, indep_vars_list[[model_name]], data_P3_)
    models[[paste(dep_var, model_name, sep = "_")]] <- model
  }
}
## Warning: glm.fit: des probabilités ont été ajustées numériquement à 0 ou 1
# Transformer la liste en un format utilisable par stargazer
models_flat <- unname(models)  # Supprime les noms pour éviter les erreurs


# Générer le tableau avec Stargazer et enregistrer dans le dossier 'rapport' avec 'here'
stargazer(models_flat, type = "html", title = "Résultats des régressions logistiques",
          style = "aer", digits = 2, out = here("rapport", "resultats_regression.html"))
## 
## <table style="text-align:center"><caption><strong>Résultats des régressions logistiques</strong></caption>
## <tr><td colspan="33" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"></td><td colspan="4">Quiter_nid_rec</td><td colspan="4">Emploi_TPL_rec</td><td colspan="4">Vivre_en_couple_rec</td><td colspan="4">Marier_1er_fois_rec</td><td colspan="4">Parent_1er_fois_rec</td><td colspan="4">Propres_responsabilites_rec</td><td colspan="4">Propres_decisions_rec</td><td colspan="4">Independance_finance_rec</td></tr>
## <tr><td style="text-align:left"></td><td>(1)</td><td>(2)</td><td>(3)</td><td>(4)</td><td>(5)</td><td>(6)</td><td>(7)</td><td>(8)</td><td>(9)</td><td>(10)</td><td>(11)</td><td>(12)</td><td>(13)</td><td>(14)</td><td>(15)</td><td>(16)</td><td>(17)</td><td>(18)</td><td>(19)</td><td>(20)</td><td>(21)</td><td>(22)</td><td>(23)</td><td>(24)</td><td>(25)</td><td>(26)</td><td>(27)</td><td>(28)</td><td>(29)</td><td>(30)</td><td>(31)</td><td>(32)</td></tr>
## <tr><td colspan="33" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">Usage_MSOui</td><td>1.25<sup>***</sup></td><td></td><td></td><td>1.02</td><td>1.38<sup>***</sup></td><td></td><td></td><td>0.05</td><td>-0.17</td><td></td><td></td><td>-2.15<sup>*</sup></td><td>-1.16<sup>***</sup></td><td></td><td></td><td>-1.50</td><td>-0.82<sup>***</sup></td><td></td><td></td><td>-0.95</td><td>2.58<sup>***</sup></td><td></td><td></td><td>14.03</td><td>2.74<sup>***</sup></td><td></td><td></td><td>13.69</td><td>2.14<sup>***</sup></td><td></td><td></td><td>30.26</td></tr>
## <tr><td style="text-align:left"></td><td>(0.30)</td><td></td><td></td><td>(1.46)</td><td>(0.32)</td><td></td><td></td><td>(1.45)</td><td>(0.28)</td><td></td><td></td><td>(1.21)</td><td>(0.28)</td><td></td><td></td><td>(1.33)</td><td>(0.30)</td><td></td><td></td><td>(1.09)</td><td>(0.38)</td><td></td><td></td><td>(855.46)</td><td>(0.41)</td><td></td><td></td><td>(830.95)</td><td>(0.38)</td><td></td><td></td><td>(2,135.27)</td></tr>
## <tr><td style="text-align:left"></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
## <tr><td style="text-align:left">Nbr_compte_MS3 à 5 comptes</td><td></td><td>0.20</td><td></td><td>-0.04</td><td></td><td>0.30</td><td></td><td>-0.09</td><td></td><td>-0.23</td><td></td><td>-0.54<sup>**</sup></td><td></td><td>0.12</td><td></td><td>0.24</td><td></td><td>-0.10</td><td></td><td>-0.17</td><td></td><td>-0.02</td><td></td><td>-0.10</td><td></td><td>-0.34</td><td></td><td>-0.33</td><td></td><td>0.38</td><td></td><td>-0.04</td></tr>
## <tr><td style="text-align:left"></td><td></td><td>(0.25)</td><td></td><td>(0.31)</td><td></td><td>(0.30)</td><td></td><td>(0.36)</td><td></td><td>(0.18)</td><td></td><td>(0.24)</td><td></td><td>(0.20)</td><td></td><td>(0.26)</td><td></td><td>(0.18)</td><td></td><td>(0.24)</td><td></td><td>(0.55)</td><td></td><td>(0.68)</td><td></td><td>(0.67)</td><td></td><td>(0.90)</td><td></td><td>(0.47)</td><td></td><td>(0.59)</td></tr>
## <tr><td style="text-align:left"></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
## <tr><td style="text-align:left">Nbr_compte_MS6 comptes et plus</td><td></td><td>-0.11</td><td></td><td>-0.61</td><td></td><td>0.33</td><td></td><td>-0.67</td><td></td><td>-0.82<sup>***</sup></td><td></td><td>-1.17<sup>***</sup></td><td></td><td>-0.53</td><td></td><td>-0.29</td><td></td><td>-0.42</td><td></td><td>-0.43</td><td></td><td>0.88</td><td></td><td>0.86</td><td></td><td>0.35</td><td></td><td>0.27</td><td></td><td>0.24</td><td></td><td>-0.27</td></tr>
## <tr><td style="text-align:left"></td><td></td><td>(0.35)</td><td></td><td>(0.46)</td><td></td><td>(0.46)</td><td></td><td>(0.57)</td><td></td><td>(0.27)</td><td></td><td>(0.35)</td><td></td><td>(0.33)</td><td></td><td>(0.41)</td><td></td><td>(0.26)</td><td></td><td>(0.35)</td><td></td><td>(1.10)</td><td></td><td>(1.28)</td><td></td><td>(1.16)</td><td></td><td>(1.39)</td><td></td><td>(0.69)</td><td></td><td>(0.92)</td></tr>
## <tr><td style="text-align:left"></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
## <tr><td style="text-align:left">Nbr_compte_MSAucun</td><td></td><td>-1.07<sup>***</sup></td><td></td><td>-0.32</td><td></td><td>-1.12<sup>***</sup></td><td></td><td>-1.05</td><td></td><td>-0.19</td><td></td><td>-1.16</td><td></td><td>1.03<sup>***</sup></td><td></td><td>-0.43</td><td></td><td>0.63<sup>**</sup></td><td></td><td>0.05</td><td></td><td>-2.51<sup>***</sup></td><td></td><td>-2.17<sup>*</sup></td><td></td><td>-2.95<sup>***</sup></td><td></td><td>-2.80<sup>**</sup></td><td></td><td>-1.75<sup>***</sup></td><td></td><td>14.08</td></tr>
## <tr><td style="text-align:left"></td><td></td><td>(0.33)</td><td></td><td>(1.15)</td><td></td><td>(0.37)</td><td></td><td>(1.20)</td><td></td><td>(0.30)</td><td></td><td>(0.91)</td><td></td><td>(0.30)</td><td></td><td>(1.13)</td><td></td><td>(0.31)</td><td></td><td>(0.85)</td><td></td><td>(0.54)</td><td></td><td>(1.30)</td><td></td><td>(0.65)</td><td></td><td>(1.38)</td><td></td><td>(0.48)</td><td></td><td>(1,559.36)</td></tr>
## <tr><td style="text-align:left"></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
## <tr><td style="text-align:left">Nbr_compte_MS_regulierDeux</td><td></td><td></td><td>0.84<sup>***</sup></td><td>-0.35</td><td></td><td></td><td>0.87<sup>**</sup></td><td>-0.08</td><td></td><td></td><td>0.05</td><td>1.23</td><td></td><td></td><td>-0.89<sup>***</sup></td><td>-0.05</td><td></td><td></td><td>-0.49</td><td>0.48</td><td></td><td></td><td>2.19<sup>***</sup></td><td>-13.78</td><td></td><td></td><td>2.97<sup>***</sup></td><td>-13.13</td><td></td><td></td><td>1.87<sup>***</sup></td><td>-14.13</td></tr>
## <tr><td style="text-align:left"></td><td></td><td></td><td>(0.32)</td><td>(1.11)</td><td></td><td></td><td>(0.35)</td><td>(1.15)</td><td></td><td></td><td>(0.29)</td><td>(0.87)</td><td></td><td></td><td>(0.29)</td><td>(0.86)</td><td></td><td></td><td>(0.30)</td><td>(0.79)</td><td></td><td></td><td>(0.49)</td><td>(855.46)</td><td></td><td></td><td>(0.66)</td><td>(830.95)</td><td></td><td></td><td>(0.48)</td><td>(1,458.68)</td></tr>
## <tr><td style="text-align:left"></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
## <tr><td style="text-align:left">Nbr_compte_MS_regulierPlus de quatre</td><td></td><td></td><td>1.41<sup>***</sup></td><td>0.57</td><td></td><td></td><td>2.52<sup>***</sup></td><td>1.98</td><td></td><td></td><td>-0.37</td><td>1.39</td><td></td><td></td><td>-1.40<sup>***</sup></td><td>-0.39</td><td></td><td></td><td>-0.88<sup>**</sup></td><td>0.29</td><td></td><td></td><td>2.35<sup>***</sup></td><td>-13.80</td><td></td><td></td><td>2.97<sup>***</sup></td><td>-13.02</td><td></td><td></td><td>2.16<sup>***</sup></td><td>-13.72</td></tr>
## <tr><td style="text-align:left"></td><td></td><td></td><td>(0.46)</td><td>(1.19)</td><td></td><td></td><td>(0.77)</td><td>(1.37)</td><td></td><td></td><td>(0.35)</td><td>(0.91)</td><td></td><td></td><td>(0.39)</td><td>(0.92)</td><td></td><td></td><td>(0.36)</td><td>(0.83)</td><td></td><td></td><td>(0.78)</td><td>(855.46)</td><td></td><td></td><td>(1.05)</td><td>(830.95)</td><td></td><td></td><td>(0.78)</td><td>(1,458.68)</td></tr>
## <tr><td style="text-align:left"></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
## <tr><td style="text-align:left">Nbr_compte_MS_regulierQuatre</td><td></td><td></td><td>1.00<sup>***</sup></td><td>-0.07</td><td></td><td></td><td>1.92<sup>***</sup></td><td>1.13</td><td></td><td></td><td>-0.20</td><td>1.32</td><td></td><td></td><td>-1.44<sup>***</sup></td><td>-0.63</td><td></td><td></td><td>-1.06<sup>***</sup></td><td>0.02</td><td></td><td></td><td>2.70<sup>***</sup></td><td>-13.30</td><td></td><td></td><td>2.19<sup>***</sup></td><td>-13.82</td><td></td><td></td><td>1.57<sup>***</sup></td><td>-14.39</td></tr>
## <tr><td style="text-align:left"></td><td></td><td></td><td>(0.38)</td><td>(1.15)</td><td></td><td></td><td>(0.54)</td><td>(1.24)</td><td></td><td></td><td>(0.32)</td><td>(0.89)</td><td></td><td></td><td>(0.36)</td><td>(0.90)</td><td></td><td></td><td>(0.34)</td><td>(0.81)</td><td></td><td></td><td>(0.77)</td><td>(855.46)</td><td></td><td></td><td>(0.66)</td><td>(830.95)</td><td></td><td></td><td>(0.55)</td><td>(1,458.68)</td></tr>
## <tr><td style="text-align:left"></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
## <tr><td style="text-align:left">Nbr_compte_MS_regulierTrois</td><td></td><td></td><td>1.52<sup>***</sup></td><td>0.43</td><td></td><td></td><td>1.43<sup>***</sup></td><td>0.60</td><td></td><td></td><td>0.15</td><td>1.64<sup>*</sup></td><td></td><td></td><td>-0.92<sup>***</sup></td><td>-0.13</td><td></td><td></td><td>-0.58<sup>*</sup></td><td>0.50</td><td></td><td></td><td>2.57<sup>***</sup></td><td>-13.46</td><td></td><td></td><td>2.48<sup>***</sup></td><td>-13.59</td><td></td><td></td><td>3.10<sup>***</sup></td><td>-12.86</td></tr>
## <tr><td style="text-align:left"></td><td></td><td></td><td>(0.36)</td><td>(1.14)</td><td></td><td></td><td>(0.40)</td><td>(1.18)</td><td></td><td></td><td>(0.30)</td><td>(0.88)</td><td></td><td></td><td>(0.30)</td><td>(0.88)</td><td></td><td></td><td>(0.31)</td><td>(0.80)</td><td></td><td></td><td>(0.59)</td><td>(855.46)</td><td></td><td></td><td>(0.59)</td><td>(830.95)</td><td></td><td></td><td>(0.78)</td><td>(1,458.68)</td></tr>
## <tr><td style="text-align:left"></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
## <tr><td style="text-align:left">Nbr_compte_MS_regulierUn</td><td></td><td></td><td>1.19<sup>***</sup></td><td>-0.02</td><td></td><td></td><td>1.24<sup>***</sup></td><td>0.26</td><td></td><td></td><td>-0.01</td><td>0.98</td><td></td><td></td><td>-1.06<sup>***</sup></td><td>-0.14</td><td></td><td></td><td>-0.80<sup>**</sup></td><td>0.11</td><td></td><td></td><td>2.45<sup>***</sup></td><td>-13.52</td><td></td><td></td><td>2.36<sup>***</sup></td><td>-13.81</td><td></td><td></td><td>1.54<sup>***</sup></td><td>-14.48</td></tr>
## <tr><td style="text-align:left"></td><td></td><td></td><td>(0.38)</td><td>(1.13)</td><td></td><td></td><td>(0.43)</td><td>(1.17)</td><td></td><td></td><td>(0.32)</td><td>(0.87)</td><td></td><td></td><td>(0.33)</td><td>(0.88)</td><td></td><td></td><td>(0.32)</td><td>(0.80)</td><td></td><td></td><td>(0.66)</td><td>(855.46)</td><td></td><td></td><td>(0.66)</td><td>(830.95)</td><td></td><td></td><td>(0.52)</td><td>(1,458.68)</td></tr>
## <tr><td style="text-align:left"></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
## <tr><td style="text-align:left">Constant</td><td>0.51<sup>*</sup></td><td>1.66<sup>***</sup></td><td>0.62<sup>**</sup></td><td>0.84</td><td>0.92<sup>***</sup></td><td>2.09<sup>***</sup></td><td>1.00<sup>***</sup></td><td>1.96</td><td>0.36</td><td>0.45<sup>***</sup></td><td>0.22</td><td>1.52</td><td>0.14</td><td>-1.03<sup>***</sup></td><td>0.03</td><td>0.57</td><td>0.75<sup>***</sup></td><td>0.04</td><td>0.62<sup>**</sup></td><td>0.70</td><td>1.01<sup>***</sup></td><td>3.57<sup>***</sup></td><td>1.16<sup>***</sup></td><td>3.17<sup>**</sup></td><td>1.10<sup>***</sup></td><td>4.09<sup>***</sup></td><td>1.25<sup>***</sup></td><td>3.90<sup>***</sup></td><td>1.20<sup>***</sup></td><td>3.08<sup>***</sup></td><td>1.35<sup>***</sup></td><td>-12.88</td></tr>
## <tr><td style="text-align:left"></td><td>(0.28)</td><td>(0.20)</td><td>(0.26)</td><td>(1.18)</td><td>(0.30)</td><td>(0.24)</td><td>(0.28)</td><td>(1.24)</td><td>(0.27)</td><td>(0.15)</td><td>(0.25)</td><td>(0.95)</td><td>(0.27)</td><td>(0.17)</td><td>(0.25)</td><td>(1.16)</td><td>(0.29)</td><td>(0.15)</td><td>(0.26)</td><td>(0.90)</td><td>(0.30)</td><td>(0.45)</td><td>(0.30)</td><td>(1.34)</td><td>(0.31)</td><td>(0.58)</td><td>(0.30)</td><td>(1.42)</td><td>(0.32)</td><td>(0.36)</td><td>(0.31)</td><td>(1,559.36)</td></tr>
## <tr><td style="text-align:left"></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
## <tr><td style="text-align:left">Observations</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td><td>723</td></tr>
## <tr><td style="text-align:left">Log Likelihood</td><td>-315.41</td><td>-315.39</td><td>-313.21</td><td>-311.17</td><td>-237.53</td><td>-237.03</td><td>-232.36</td><td>-231.00</td><td>-497.08</td><td>-492.34</td><td>-495.05</td><td>-487.95</td><td>-424.60</td><td>-423.55</td><td>-422.54</td><td>-419.93</td><td>-497.10</td><td>-496.17</td><td>-494.40</td><td>-492.76</td><td>-115.32</td><td>-113.94</td><td>-116.87</td><td>-113.14</td><td>-99.44</td><td>-97.93</td><td>-100.55</td><td>-96.88</td><td>-130.39</td><td>-131.35</td><td>-129.13</td><td>-127.15</td></tr>
## <tr><td style="text-align:left">Akaike Inf. Crit.</td><td>634.83</td><td>638.78</td><td>638.43</td><td>642.33</td><td>479.06</td><td>482.07</td><td>476.72</td><td>481.99</td><td>998.15</td><td>992.68</td><td>1,002.10</td><td>995.89</td><td>853.19</td><td>855.10</td><td>857.08</td><td>859.86</td><td>998.19</td><td>1,000.33</td><td>1,000.80</td><td>1,005.52</td><td>234.64</td><td>235.87</td><td>245.73</td><td>246.29</td><td>202.87</td><td>203.86</td><td>213.09</td><td>213.77</td><td>264.78</td><td>270.71</td><td>270.26</td><td>274.29</td></tr>
## <tr><td colspan="33" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"><em>Notes:</em></td><td colspan="32" style="text-align:left"><sup>***</sup>Significant at the 1 percent level.</td></tr>
## <tr><td style="text-align:left"></td><td colspan="32" style="text-align:left"><sup>**</sup>Significant at the 5 percent level.</td></tr>
## <tr><td style="text-align:left"></td><td colspan="32" style="text-align:left"><sup>*</sup>Significant at the 10 percent level.</td></tr>
## </table>

II. Forest plot avec sjPlot

1. Variable Utilisation des médias

#install.packages("sjPlot")
library(sjPlot)
# Fonction pour générer un modèle logistique
generate_logistic_model <- function(dep_var, indep_var, data) {
  formula <- as.formula(paste(dep_var, "~", indep_var))
  model <- glm(formula, data = data, family = binomial)
  return(model)
}

# Liste des variables dépendantes
dependent_vars <- c("Quiter_nid_rec", "Emploi_TPL_rec", "Vivre_en_couple_rec", 
                    "Marier_1er_fois_rec", "Parent_1er_fois_rec", 
                    "Propres_responsabilites_rec", "Propres_decisions_rec", 
                    "Independance_finance_rec")

# Variable indépendante (Usage_MS uniquement)
indep_vars_1a <- "Usage_MS"

# Créer une liste vide pour stocker les modèles
model_list <- list()
m_labels <- c()  # Liste pour stocker les labels des modèles

# Appliquer les régressions pour chaque variable dépendante
for (dep_var in dependent_vars) {
  model_list[[paste0(dep_var, "_Usage_MS")]] <- generate_logistic_model(dep_var, indep_vars_1a, data_P3_)
  
  # Ajouter les labels incrémentés dans m_labels
  m_labels <- c(m_labels, paste0(dep_var, ""))  # Label pour Usage_MS uniquement
}

# Vérification des modèles et des labels
#print(names(model_list))  # Afficher les noms des modèles
#print(m_labels)  # Afficher les labels

# Appliquer plot_models avec les modèles et les labels
plot_models(
  model_list,
  axis.labels = c("Usage des Médias Sociaux"),  # Seule variable indépendante
  m.labels = m_labels,  # Utilisation des labels incrémentés
  show.values = FALSE,  # Ne pas afficher les valeurs numériques des coefficients
  show.p = FALSE,  # Ne pas afficher les p-valeurs
  p.shape = TRUE,  # Afficher les formes des points au lieu des valeurs
  grid = TRUE  # Afficher une grille
)

# Générer le graphique et enregistrer dans le dossier 'figures/forestplot'
ggsave(here("figures", "forestplot", "Utilisation des médias_forestplot.png"), 
       plot = last_plot(), width = 10, height = 6)

2. Variable : nombre de de compte des médias

# Fonction pour générer un modèle logistique
generate_logistic_model <- function(dep_var, indep_var, data) {
  formula <- as.formula(paste(dep_var, "~", indep_var))
  model <- glm(formula, data = data, family = binomial)
  return(model)
}

# Liste des variables dépendantes
dependent_vars <- c("Quiter_nid_rec", "Emploi_TPL_rec", "Vivre_en_couple_rec", 
                    "Marier_1er_fois_rec", "Parent_1er_fois_rec", 
                    "Propres_responsabilites_rec", "Propres_decisions_rec", 
                    "Independance_finance_rec")

# Variable indépendante : Nombre de Comptes MS
indep_vars_1b <- "Nbr_compte_MS"  # Ajout de "Nbr_compte_MS"

# Créer une liste vide pour stocker les modèles
model_list <- list()
m_labels <- c()  # Liste pour stocker les labels des modèles

# Appliquer les régressions pour chaque variable dépendante
for (dep_var in dependent_vars) {
  model_list[[paste0(dep_var, "_Nbr_compte_MS")]] <- generate_logistic_model(dep_var, indep_vars_1b, data_P3_)
  
  # Ajouter les labels incrémentés dans m_labels
  m_labels <- c(m_labels,
                paste0(dep_var, ""))  # Label pour Nbr_compte_MS
}

# Vérification des modèles et des labels
#print(names(model_list))  # Afficher les noms des modèles
#print(m_labels)  # Afficher les labels

# Appliquer plot_models avec les modèles et les labels
plot_models(
  model_list,
  m.labels = m_labels,  # Utilisation des labels incrémentés
  show.values = FALSE,  # Ne pas afficher les valeurs numériques des coefficients
  show.p = FALSE,  # Ne pas afficher les p-valeurs
  p.shape = TRUE,  # Afficher les formes des points au lieu des valeurs
  grid = TRUE  # Afficher une grille
)

# Générer le graphique et enregistrer dans le dossier 'figures/forestplot'
ggsave(here("figures", "forestplot", "nombre de de compte des médias_forestplot.png"), 
       plot = last_plot(), width = 10, height = 6)

3. Variable utilisation de compte regulier

# Fonction pour générer un modèle logistique
generate_logistic_model <- function(dep_var, indep_var, data) {
  formula <- as.formula(paste(dep_var, "~", indep_var))
  model <- glm(formula, data = data, family = binomial)
  return(model)
}

# Liste des variables dépendantes
dependent_vars <- c("Quiter_nid_rec", "Emploi_TPL_rec", "Vivre_en_couple_rec", 
                    "Marier_1er_fois_rec", "Parent_1er_fois_rec", 
                    "Propres_responsabilites_rec", "Propres_decisions_rec", 
                    "Independance_finance_rec")

# Variable indépendante : Nombre de Comptes MS Régulier
indep_vars_1c <- "Nbr_compte_MS_regulier"  # Remplacement par "Nbr_compte_MS_regulier"

# Créer une liste vide pour stocker les modèles
model_list <- list()
m_labels <- c()  # Liste pour stocker les labels des modèles

# Appliquer les régressions pour chaque variable dépendante
for (dep_var in dependent_vars) {
  model <- generate_logistic_model(dep_var, indep_vars_1c, data_P3_)
  model_list[[paste0(dep_var, "_Nbr_compte_MS_regulier")]] <- model
  
  # Ajouter les labels incrémentés dans m_labels
  m_labels <- c(m_labels,
                paste0(dep_var, ""))  # Label pour Nbr_compte_MS_regulier
  
  # Afficher les résultats de la régression pour chaque modèle
  cat("\nRésultats de la régression pour", dep_var, "avec Nbr_compte_MS_regulier:\n")
  print(summary(model))  # Afficher le résumé du modèle
}
## 
## Résultats de la régression pour Quiter_nid_rec avec Nbr_compte_MS_regulier:
## 
## Call:
## glm(formula = formula, family = binomial, data = data)
## 
## Coefficients:
##                                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                            0.6225     0.2643   2.356  0.01850 *  
## Nbr_compte_MS_regulierDeux             0.8438     0.3184   2.650  0.00805 ** 
## Nbr_compte_MS_regulierPlus de quatre   1.4089     0.4596   3.065  0.00217 ** 
## Nbr_compte_MS_regulierQuatre           0.9993     0.3804   2.627  0.00861 ** 
## Nbr_compte_MS_regulierTrois            1.5241     0.3632   4.197 2.71e-05 ***
## Nbr_compte_MS_regulierUn               1.1898     0.3776   3.151  0.00162 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 646.66  on 722  degrees of freedom
## Residual deviance: 626.43  on 717  degrees of freedom
## AIC: 638.43
## 
## Number of Fisher Scoring iterations: 4
## 
## 
## Résultats de la régression pour Emploi_TPL_rec avec Nbr_compte_MS_regulier:
## 
## Call:
## glm(formula = formula, family = binomial, data = data)
## 
## Coefficients:
##                                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                            0.9954     0.2838   3.507 0.000453 ***
## Nbr_compte_MS_regulierDeux             0.8653     0.3490   2.479 0.013171 *  
## Nbr_compte_MS_regulierPlus de quatre   2.5161     0.7717   3.261 0.001112 ** 
## Nbr_compte_MS_regulierQuatre           1.9169     0.5398   3.551 0.000384 ***
## Nbr_compte_MS_regulierTrois            1.4281     0.3979   3.589 0.000332 ***
## Nbr_compte_MS_regulierUn               1.2414     0.4257   2.916 0.003540 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 490.39  on 722  degrees of freedom
## Residual deviance: 464.72  on 717  degrees of freedom
## AIC: 476.72
## 
## Number of Fisher Scoring iterations: 6
## 
## 
## Résultats de la régression pour Vivre_en_couple_rec avec Nbr_compte_MS_regulier:
## 
## Call:
## glm(formula = formula, family = binomial, data = data)
## 
## Coefficients:
##                                      Estimate Std. Error z value Pr(>|z|)
## (Intercept)                           0.22314    0.25355   0.880    0.379
## Nbr_compte_MS_regulierDeux            0.04773    0.28961   0.165    0.869
## Nbr_compte_MS_regulierPlus de quatre -0.36833    0.35009  -1.052    0.293
## Nbr_compte_MS_regulierQuatre         -0.20252    0.32485  -0.623    0.533
## Nbr_compte_MS_regulierTrois           0.15333    0.29728   0.516    0.606
## Nbr_compte_MS_regulierUn             -0.01183    0.31586  -0.037    0.970
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 994.5  on 722  degrees of freedom
## Residual deviance: 990.1  on 717  degrees of freedom
## AIC: 1002.1
## 
## Number of Fisher Scoring iterations: 4
## 
## 
## Résultats de la régression pour Marier_1er_fois_rec avec Nbr_compte_MS_regulier:
## 
## Call:
## glm(formula = formula, family = binomial, data = data)
## 
## Coefficients:
##                                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                           0.03175    0.25201   0.126 0.899745    
## Nbr_compte_MS_regulierDeux           -0.88822    0.29409  -3.020 0.002525 ** 
## Nbr_compte_MS_regulierPlus de quatre -1.40002    0.39130  -3.578 0.000346 ***
## Nbr_compte_MS_regulierQuatre         -1.44402    0.35911  -4.021 5.79e-05 ***
## Nbr_compte_MS_regulierTrois          -0.92375    0.30283  -3.050 0.002285 ** 
## Nbr_compte_MS_regulierUn             -1.06137    0.32977  -3.219 0.001288 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 865.88  on 722  degrees of freedom
## Residual deviance: 845.08  on 717  degrees of freedom
## AIC: 857.08
## 
## Number of Fisher Scoring iterations: 4
## 
## 
## Résultats de la régression pour Parent_1er_fois_rec avec Nbr_compte_MS_regulier:
## 
## Call:
## glm(formula = formula, family = binomial, data = data)
## 
## Coefficients:
##                                      Estimate Std. Error z value Pr(>|z|)   
## (Intercept)                            0.6225     0.2643   2.356  0.01850 * 
## Nbr_compte_MS_regulierDeux            -0.4877     0.2986  -1.633  0.10240   
## Nbr_compte_MS_regulierPlus de quatre  -0.8849     0.3589  -2.465  0.01368 * 
## Nbr_compte_MS_regulierQuatre          -1.0625     0.3363  -3.159  0.00158 **
## Nbr_compte_MS_regulierTrois           -0.5760     0.3051  -1.888  0.05907 . 
## Nbr_compte_MS_regulierUn              -0.7984     0.3244  -2.462  0.01383 * 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 1002.3  on 722  degrees of freedom
## Residual deviance:  988.8  on 717  degrees of freedom
## AIC: 1000.8
## 
## Number of Fisher Scoring iterations: 4
## 
## 
## Résultats de la régression pour Propres_responsabilites_rec avec Nbr_compte_MS_regulier:
## 
## Call:
## glm(formula = formula, family = binomial, data = data)
## 
## Coefficients:
##                                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                            1.1632     0.2958   3.932 8.42e-05 ***
## Nbr_compte_MS_regulierDeux             2.1942     0.4851   4.523 6.09e-06 ***
## Nbr_compte_MS_regulierPlus de quatre   2.3484     0.7762   3.026 0.002481 ** 
## Nbr_compte_MS_regulierQuatre           2.6976     0.7733   3.488 0.000486 ***
## Nbr_compte_MS_regulierTrois            2.5745     0.5860   4.393 1.12e-05 ***
## Nbr_compte_MS_regulierUn               2.4478     0.6556   3.734 0.000189 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 268.21  on 722  degrees of freedom
## Residual deviance: 233.73  on 717  degrees of freedom
## AIC: 245.73
## 
## Number of Fisher Scoring iterations: 6
## 
## 
## Résultats de la régression pour Propres_decisions_rec avec Nbr_compte_MS_regulier:
## 
## Call:
## glm(formula = formula, family = binomial, data = data)
## 
## Coefficients:
##                                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                            1.2528     0.3030   4.134 3.57e-05 ***
## Nbr_compte_MS_regulierDeux             2.9716     0.6558   4.531 5.86e-06 ***
## Nbr_compte_MS_regulierPlus de quatre   2.9667     1.0519   2.820 0.004798 ** 
## Nbr_compte_MS_regulierQuatre           2.1919     0.6602   3.320 0.000899 ***
## Nbr_compte_MS_regulierTrois            2.4849     0.5897   4.214 2.51e-05 ***
## Nbr_compte_MS_regulierUn               2.3582     0.6589   3.579 0.000345 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 236.97  on 722  degrees of freedom
## Residual deviance: 201.09  on 717  degrees of freedom
## AIC: 213.09
## 
## Number of Fisher Scoring iterations: 7
## 
## 
## Résultats de la régression pour Independance_finance_rec avec Nbr_compte_MS_regulier:
## 
## Call:
## glm(formula = formula, family = binomial, data = data)
## 
## Coefficients:
##                                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                            1.3471     0.3113   4.327 1.51e-05 ***
## Nbr_compte_MS_regulierDeux             1.8718     0.4764   3.929 8.52e-05 ***
## Nbr_compte_MS_regulierPlus de quatre   2.1645     0.7822   2.767  0.00566 ** 
## Nbr_compte_MS_regulierQuatre           1.5653     0.5548   2.821  0.00478 ** 
## Nbr_compte_MS_regulierTrois            3.0956     0.7764   3.987 6.69e-05 ***
## Nbr_compte_MS_regulierUn               1.5433     0.5223   2.955  0.00313 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 286.17  on 722  degrees of freedom
## Residual deviance: 258.26  on 717  degrees of freedom
## AIC: 270.26
## 
## Number of Fisher Scoring iterations: 7
# Vérification des modèles et des labels
#print(names(model_list))  # Afficher les noms des modèles
#print(m_labels)  # Afficher les labels

# Appliquer plot_models avec les modèles et les labels
plot_models(
  model_list,
    # Ajout de Nbr_compte_MS_regulier
  m.labels = m_labels,  # Utilisation des labels incrémentés
  show.values = FALSE,  # Ne pas afficher les valeurs numériques des coefficients
  show.p = FALSE,  # Ne pas afficher les p-valeurs
  p.shape = TRUE,  # Afficher les formes des points au lieu des valeurs
  grid = TRUE  # Afficher une grille
)

# Générer le graphique et enregistrer dans le dossier 'figures/forestplot'
ggsave(here("figures", "forestplot", "nombre de de compte regulier_forestplot.png"), 
       plot = last_plot(), width = 10, height = 6)

05. variables concernant les médias

# Fonction pour générer un modèle logistique
generate_logistic_model <- function(dep_var, indep_var, data) {
  formula <- as.formula(paste(dep_var, "~", indep_var))
  model <- glm(formula, data = data, family = binomial)
  return(model)
}

# Liste des variables dépendantes
dependent_vars <- c("Quiter_nid_rec", "Emploi_TPL_rec", "Vivre_en_couple_rec", 
                    "Marier_1er_fois_rec", "Parent_1er_fois_rec", 
                    "Propres_responsabilites_rec", "Propres_decisions_rec", 
                    "Independance_finance_rec")

# Variables indépendantes
indep_vars_1a <- "Usage_MS"
indep_vars_1b <- "Nbr_compte_MS"
indep_vars_1c <- "Nbr_compte_MS_regulier"

# Créer une liste vide pour stocker les modèles
model_list <- list()
m_labels <- c()  # Liste pour stocker les labels des modèles

# Appliquer les régressions pour chaque variable dépendante
for (dep_var in dependent_vars) {
  model_list[[paste0(dep_var, "_Usage_MS")]] <- generate_logistic_model(dep_var, indep_vars_1a, data_P3_)
  model_list[[paste0(dep_var, "_Nbr_compte_MS")]] <- generate_logistic_model(dep_var, indep_vars_1b, data_P3_)
  model_list[[paste0(dep_var, "_Nbr_compte_MS_regulier")]] <- generate_logistic_model(dep_var, indep_vars_1c, data_P3_)
  
  # Ajouter les labels incrémentés dans m_labels
  m_labels <- c(m_labels,
                paste0(dep_var, "_1"),  # Label pour Usage_MS
                paste0(dep_var, "_2"),  # Label pour Nbr_compte_MS
                paste0(dep_var, "_3"))  # Label pour Nbr_compte_MS_regulier
}

# Vérification des modèles et des labels
print(names(model_list))  # Afficher les noms des modèles
##  [1] "Quiter_nid_rec_Usage_MS"                           
##  [2] "Quiter_nid_rec_Nbr_compte_MS"                      
##  [3] "Quiter_nid_rec_Nbr_compte_MS_regulier"             
##  [4] "Emploi_TPL_rec_Usage_MS"                           
##  [5] "Emploi_TPL_rec_Nbr_compte_MS"                      
##  [6] "Emploi_TPL_rec_Nbr_compte_MS_regulier"             
##  [7] "Vivre_en_couple_rec_Usage_MS"                      
##  [8] "Vivre_en_couple_rec_Nbr_compte_MS"                 
##  [9] "Vivre_en_couple_rec_Nbr_compte_MS_regulier"        
## [10] "Marier_1er_fois_rec_Usage_MS"                      
## [11] "Marier_1er_fois_rec_Nbr_compte_MS"                 
## [12] "Marier_1er_fois_rec_Nbr_compte_MS_regulier"        
## [13] "Parent_1er_fois_rec_Usage_MS"                      
## [14] "Parent_1er_fois_rec_Nbr_compte_MS"                 
## [15] "Parent_1er_fois_rec_Nbr_compte_MS_regulier"        
## [16] "Propres_responsabilites_rec_Usage_MS"              
## [17] "Propres_responsabilites_rec_Nbr_compte_MS"         
## [18] "Propres_responsabilites_rec_Nbr_compte_MS_regulier"
## [19] "Propres_decisions_rec_Usage_MS"                    
## [20] "Propres_decisions_rec_Nbr_compte_MS"               
## [21] "Propres_decisions_rec_Nbr_compte_MS_regulier"      
## [22] "Independance_finance_rec_Usage_MS"                 
## [23] "Independance_finance_rec_Nbr_compte_MS"            
## [24] "Independance_finance_rec_Nbr_compte_MS_regulier"
print(m_labels)  # Afficher les labels
##  [1] "Quiter_nid_rec_1"              "Quiter_nid_rec_2"             
##  [3] "Quiter_nid_rec_3"              "Emploi_TPL_rec_1"             
##  [5] "Emploi_TPL_rec_2"              "Emploi_TPL_rec_3"             
##  [7] "Vivre_en_couple_rec_1"         "Vivre_en_couple_rec_2"        
##  [9] "Vivre_en_couple_rec_3"         "Marier_1er_fois_rec_1"        
## [11] "Marier_1er_fois_rec_2"         "Marier_1er_fois_rec_3"        
## [13] "Parent_1er_fois_rec_1"         "Parent_1er_fois_rec_2"        
## [15] "Parent_1er_fois_rec_3"         "Propres_responsabilites_rec_1"
## [17] "Propres_responsabilites_rec_2" "Propres_responsabilites_rec_3"
## [19] "Propres_decisions_rec_1"       "Propres_decisions_rec_2"      
## [21] "Propres_decisions_rec_3"       "Independance_finance_rec_1"   
## [23] "Independance_finance_rec_2"    "Independance_finance_rec_3"
# Appliquer plot_models avec les modèles et les labels
plot_models(
  model_list,
    # Sans la combinaison
  m.labels = m_labels,  # Utilisation des labels incrémentés
  show.values = FALSE,  # Ne pas afficher les valeurs numériques des coefficients
  show.p = FALSE,  # Ne pas afficher les p-valeurs
  p.shape = TRUE,  # Afficher les formes des points au lieu des valeurs
  grid = TRUE  # Afficher une grille
)
## Warning in RColorBrewer::brewer.pal(n, pal): n too large, allowed maximum for palette Set1 is 9
## Returning the palette you asked for with that many colors

# Générer le graphique et enregistrer dans le dossier 'figures/forestplot'
ggsave(here("figures", "forestplot", "Toute _les variables_utilisation des médias_forestplot.png"), 
       plot = last_plot(), width = 10, height = 6)